Search code examples
httppostdartrequestjson-server

How to add data to backend with Dart http


I'm trying to add some data to backend with Dart using http package. I managed to add data with post method but it just replaces all existing data. I assume that isn't how it should work. I'm using typicode json-server as a dummy backend. Is the server behaving correctly? What am I doing wrong?

My current code

import 'package:http/http.dart' as http;
import 'dart:convert';
...
String url = "localhost:3000/people/results";
var body = jsonEncode(
  {
    "results": [
      { "first": "John", "last": "Doe" }
    ]
  }
);
await http.post(url,
  body: body
);

Expected backend result

"people": {
  "results": [
    ...some previous data here...
    { "first": "John", "last": "Doe" }
  ]
}

Result

"people": {
  "results": [
    // all previous data missing
    { "first": "John", "last": "Doe" }
  ]
}

Solution

  • It seems like it's a typicode server issue because it's a dummy server and it probably discards old data, you need to create your own server to save it persistently.