4

I need to create a post request with a JSON that contains an Array. The http.post method only accepts Map<String, String>. Because of this, I cannot pass a Map with type <String,dynamic> since the Map will contain a List. Here's my code for an explicit HttpClientRequest:

String jsonString = json.encode(body); // encode map to json
List<int> bodyBytes = utf8.encode(jsonString); // utf8 encode
HttpClient client = new HttpClient();
HttpClientRequest request = await client.postUrl(uri);
// it's polite to send the body length to the server

request.headers.set('Content-Length', bodyBytes.length.toString());
request.headers.set('Authorization', '...');
request.headers.set('X-Authorization', apiKey);
request.headers.set("Accept", "application/json");
request.headers.set('Content-Type', 'form-data');
request.add(bodyBytes);
HttpClientResponse response = await request.close();
response.transform(utf8.decoder).listen((contents) {
  // handle data
  return jsonDecode(contents);
});

The problem is, the above request fails.

EDIT:

The json that is created is:

{  
   "subject":"PLEASE",
   "message":"JUST. WORK.",
   "filter":"[8]",
}

But the JSON the server expects is:

{  
   "subject":"PLEASE",
   "message":"JUST. WORK.",
   "filter":[8],
}

My main concern is how does one create a JSON array...

8
  • Not sure I understand what the problem is. What does not work in the code above? Commented Jun 7, 2018 at 9:22
  • @Edman, fails to post. I get a null response, or an invalid data response. Commented Jun 7, 2018 at 9:24
  • It's good to spell out the errors you're getting in your question, and also point where in your code you're getting the error. Also, how is that null response related to Map<String, dynamic>? Commented Jun 7, 2018 at 9:27
  • @Edman, the http.post only accepts body of type Map<String,String>, but I need to post an array in the JSON. For this, I presume I need to use a client and explicitly create a post method, as mentioned in the doc. Commented Jun 7, 2018 at 9:29
  • It's also good to give a link to that doc then. You're encoding your body to jsonString, and that's what you're passing to the http client, which sounds right. I don't understand what you mean by "post accepts body of type Map<String, String>"; I don't see where you're passing this map, the post/postUrl methods don't take a map. Commented Jun 7, 2018 at 9:41

1 Answer 1

2

Found the problem. There were two main issues:

  1. I was converting the List into a JSON string when adding to the Map. After only adding the List directly to the Map, it seemed to generate the correct JSON.
  2. The content-type must be set to application/json in the request header.

Thanks @Edman for adding some direction.

Sign up to request clarification or add additional context in comments.

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.