Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http post a map object using application/json content-type #16

Closed
DartBot opened this issue Jun 5, 2015 · 5 comments
Closed

http post a map object using application/json content-type #16

DartBot opened this issue Jun 5, 2015 · 5 comments
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@DartBot
Copy link

DartBot commented Jun 5, 2015

Originally opened as dart-lang/sdk#17636

This issue was originally filed by [email protected]


I am encountering a problem trying to use the "package:http/http.dart' pub package to post some JSON data to a web service. I have formatted the data correctly (which I confirmed by posting using the Advanced REST Client extension for Chrome) and the only difference is the Content-Type is forced to the wrong type (the web service only accepts "application/json"):

If body is a Map, it's encoded as form fields using encoding. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.

The documentation suggests:

For more fine-grained control over the request, use Request or StreamedRequest instead.

Why is the post content type limited to only form-urlencoded? Can the pub package be fixed to set the content type dynamically?

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/1343914?v=3" align="left" width="48" height="48"hspace="10"> Comment by sgjesse


Added Area-Pkg, Pkg-Http, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


The auto-encoding of Maps for [body] is just utility functionality; it makes it easier to do the common case (sending form parameters) without cutting off other possibilities. You can always post arbitrary data by passing [body] a string or a List<int>. For example:

    http.post(url,
        body: JSON.encode(data),
        headers: {'content-type': 'application/json'}));


Added NotPlanned label.

@DartBot DartBot added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) NotPlanned labels Jun 5, 2015
@DartBot DartBot closed this as completed Jun 5, 2015
@DartBot DartBot added closed-not-planned Closed as we don't intend to take action on the reported issue and removed NotPlanned labels Jun 6, 2015
@efroim102
Copy link

Is there anyway to POST json with a property that has an array value? I've been trying to send a json object to a server which works unless I add a property with the value of array (a dart List) or a property with an int property.

@ghost
Copy link

ghost commented Oct 19, 2018

@efroim102, as a general rule please open a new issue instead of reviving long dead threads.

To answer your question though, http.post(..) in itself does not handle JSON.
You can however use something like the example from above to post JSON encoded data.
Please refer to the JSON guide for how to encode and decode JSON in Dart.

@efroim102
Copy link

@cskau-g Thanks, I was able to resolve this issue using a HttpClient from the dart:io package and using jsonEncode on a Map<String, dynamic> before writing it to the HttpClientRequest stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants