-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Description
Steps to reproduce
The http.post() function is currently processing the body of the request AFTER the headers.
final response = http.post(uri, headers: headers, body: queryString);
Essentially, the post request would look something like this:
var request = http.Request('POST', uri);
request.headers['Content-Type'] = 'application/json';
request.headers['Authorization'] = encodedAPI;
request.body = queryString;
Which causes the body to automatically override the Content-Type to text/plain.
This code returns a 415 error with the API I am trying to use due to it requiring application/json.
Expected results
var request = http.Request('POST', uri);
request.body = queryString;
request.headers['Content-Type'] = 'application/json';
request.headers['Authorization'] = encodedAPI;
If the body of the http.post() was processed BEFORE the headers, this issue would not occur.
This is my current workaround that allows me to pull data from an API that requires 'application/json' as the content-type.
This code works and does not return a 415 error.
Actual results
If the http.post(uri, headers: headers, body: queryString) function were to be rewritten to perform the body check BEFORE the headers check, the 415 error I was experiencing would not have occured.
Currently, the information for http.post() look like this:
Sends an HTTP POST request with the given headers and body to the given URL.
body sets the body of the request. It can be a String, a List or a Map<String, String>. If it's a String, it's encoded using encoding and used as the body of the request. The content-type of the request will default to "text/plain"
The default behavior is currently overriding the expected behavior. Please move the body defining code before the header defining code within HTTP.POST() to resolve this issue.
Code sample
Code sample
[Paste your code here]Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]Flutter Doctor output
Doctor output
[Paste your output here]