1

I'm using http/http.dart trying to POST multipart/form-data like this :

Version: "1.0"
Token: "xxxxxxxxxx"
Ids: "1"
Ids: "2"
Ids: "3"

The problem is Dart accept map body which do not allows duplicate key. The best approach I could come up is wrap "Ids" key as array. Unfortunately I'm not in the position that can negotiate with web server dev team, so, only option I have is to use given API.

Anyone has suggestion for this issue?

CHA7RIK
  • 65
  • 1
  • 9

4 Answers4

3

I have found solution here : Flutter post api sending multiple values with same name parameter

Basically just add [$count] after the name of duplicated element.

{
    "Version": "1.0",
    "Token": "xxxxxxxxxx",
    "Ids[0]": "1",
    "Ids[1]": "2",
    "Ids[2]": "3",
}

Works for my case.

CHA7RIK
  • 65
  • 1
  • 9
1

The best solution for this is to pass a list as the value in the params map. Here's an example,

var baseUrl = 'example.com';
var path = 'posts';
var params = {
  'ids[]': ['1', '2', '3'],
  'x': 'false',
};

// http://example.com/posts?ids[]=1&ids[]=2&ids[]=3&x=false
var response = await http.get(Uri.http(baseUrl, path, params));

Note: The elements inside the list have to be strings, you can cast them to strings using,

list = list.map((e) => e.toString());
Sujit
  • 1,653
  • 2
  • 9
  • 25
0

This is https://github.com/dart-lang/http/issues/24. In the meantime, per Standard for adding multiple values of a single HTTP Header to a request or response, most HTTP header fields with the same name can be folded together with commas. In your case, since your field values presumably aren't expected to have embedded commas, you probably should be able to do:

await http.post(
  url,
  headers: {
    "Version": "1.0",
    "Token": "xxxxxxxxxx",
    "Ids": "1,2,3",
  },
  ...);
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Thanks for the answer. Just try it but it not working. Seems like server API only accept a single value per one field. – CHA7RIK Jul 27 '21 at 03:05
0

I had the same problem, but with get and it worked on http: ^0.13.5

final request = Uri.https(
  url,
  'api',
  {
    'Ids': ['1','2', '3']
  },
);

final locationResponse = await _httpClient.get(request);
Jonas Cerqueira
  • 160
  • 2
  • 8