http icon indicating copy to clipboard operation
http copied to clipboard

Web: Null body, Content-Type application/json fails, android, others are fine.

Open JohnGalt1717 opened this issue 4 years ago • 0 comments

If you create a GET request that has a header of Content-Type = "application/json" and a body of null, C# will accept this on Android and other platforms if you have it set to accept empty bodies.

The same code on Web doesn't work:

final request = http.Request(method, uri);
request.body = "";
request.headers["Content-Type"] = "application/json";
request.send();

In this case on android it goes through normally.

On web, you get a validation error that it isn't valid json that it was expecting with exactly the same code. I can see in Chrome that when request.Body is set to "" then the content type is automatically set to "text/plain". Not setting the body at all doesn't include the content-type, but then on web you get back "unsupported media type" and so does android.

So then I attempted to modify like this:

final request = http.Request(method, uri);
request.body = "{}";
request.headers["Content-Type"] = "application/json";
request.send();

Which works on android, and C# gets an empty object in the body with defaults, not null. Not ideal but I figured, if it works on web I'll live.

But on web, this results in the same issue. Inspecting the request in chrome the body of "{}" is not sent.

Ideally the first attempt should work across all platforms consistently. I can't tell what is being sent on Android but isn't being sent on web to be able to tell what the exact issue is. Chrome won't let you set the content-length header, so I can't find a work around to this issue.

Here's the android version of what works if you set the content-type = "application/json" and no body:

REQUEST HttpMethod: GET, Path: /v1/notifications/ListNotifications
Request Headers: 
Content-Type: application/json
:method: GET
:path: /v1/notifications/ListNotifications
:scheme: https
Accept: application/json
Accept-Encoding: gzip
User-Agent: Dart/2.13 (dart:io)
Content-Length: 0

Here's the chrome version that fails:

REQUEST HttpMethod: GET, Path: /v1/subscription/ListSubscriptions
Request Headers: 
Content-Type: application/json
:method: GET
:path: /v1/subscription/ListSubscriptions
:scheme: https
Accept: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Referer: http://localhost:45503/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

(note I've removed identifying information from both.

I believe the issue is that the Content-Length header is set to 0 on android, but not set to 0 on Chrome which causes it to freak out like this.

JohnGalt1717 avatar Aug 03 '21 13:08 JohnGalt1717