6

I am working on a Flutter tablet app. Once one input field is inputted, I am trying to send a post request to the server. Following is the method I am invoking for this:

    Future < http.Response > _postRequest() async {
    print(globals.novaCoord.toString() + ' XYZXYZXYZXYZXYZ');

    var url = globals.URL + 'api/nova_position';

    Map data = {
      'id_nova': '1',
      'position.x': '1',
      'position.y': '1',
      'position.z': '1',
      'position.pitch': '1',
      'position.yaw': '1',
      'position.roll': '1',
    };
    //encode Map to JSON
    var body = json.encode(data);

    http.Response response = await http.post(url,
      headers: {
        "Content-Type": "application/json"
      },
      body: body
    );
    print("${response.statusCode}");
    print("${response.body}");
    return response;
  }

And on the NodeJs server side I have this:

app.post('/api/nova_position/', async (req,res) => {
    console.log("NOVA POSITION");
    console.log(req.body.id_nova);
    const id_nova = req.body.id_nova;
    const x = req.body.position.x;
    const y = req.body.position.y;
    const z = req.body.position.z;
    const pitch = req.body.position.pitch;
    const yaw = req.body.position.yaw;
    const roll = req.body.position.roll;

    const position = await db.db.create_position(id_nova,x,y,z,pitch,yaw,roll);
});

However, on the server-side, I am receiving an empty "body" for the request I sent and I receive the following error:

(node:23939) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'x' of undefined

I need help at this point. I appreciate any help and guidance.

3
  • Are you calling http.createServer somewhere? Where's that code? Are you able to exercise your server with Postman? Commented Sep 6, 2019 at 16:04
  • Hi @RichardHeap I have a nodeJs server and even when I try with PostMan, I see that the server somehow, has an empty "body". I simply have to make sure of the server side. Commented Sep 9, 2019 at 18:34
  • Does this answer your question? Flutter POST json request body is empty/null on server side Commented Sep 13, 2022 at 14:14

3 Answers 3

12

Sending Map<String,dynamic> with http.post won't send your data properly.

To solve this, change the content-type of the header to:

Map<String, String> customHeaders = {
...
"content-type": "application/json"
...
};

Then, you can use http.post as follows:

http.post(url, headers: customHeaders, body);
Sign up to request clarification or add additional context in comments.

Comments

4

You have to jsonEncode before pushing into Post request Like

String bodyF = jsonEncode(body);

And i hope you will get your solution.

2 Comments

Yes you are correct. After I do the suggested fix, I have an updated code. I now am sure that I have an issue about the server. Received body from PostMan is empty too.
Empty body on server side may be caused by a mis-match of type between sending and receiving, which may be caused by headers. Plz see this answer for clarification!: stackoverflow.com/a/73599552/14335655
2

The solution I have found was simply to add Content-Length : x to your header, with x being the size of your json body.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.