capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

[Bug]: Android POST and PUT requests are not sending any data

Open LucaFontanot opened this issue 1 year ago • 10 comments

Capacitor Version

Latest Dependencies:

@capacitor/cli: 5.7.0 @capacitor/core: 5.7.0 @capacitor/android: 5.7.0 @capacitor/ios: 5.7.0

Installed Dependencies:

@capacitor/ios: not installed @capacitor/cli: 5.7.0 @capacitor/core: 5.7.0 @capacitor/android: 5.7.0

Other API Details

npm 9.6.7
node v18.16.0

Platforms Affected

  • [ ] iOS
  • [X] Android
  • [ ] Web

Current Behavior

Doing a simple PUT request

CapacitorHttp.put({
                url: "https://httpbin.org/put",
                headers: { 'X-Fake-Header': 'Fake-Va2lue7' },
                data: {foo:"bar"},
            })

Results in

{
    "data":{
        "args":{},
        "data":"",
        "files":{},
        "form":{},
        "headers":{
            "Accept-Encoding":"gzip",
            "Accept-Language":"it-IT,it;q=0.5",
            "Content-Length":"0",
            "Content-Type":"application/json",
            "Host":"httpbin.org",
            "User-Agent":"Dalvik/2.1 .0 (Linux; U; Android 11; LEX722 Build/RQ3A.211001.001)",
            "X-Amzn-Trace-Id":"Root=1-xx",
            "X-Fake-Header":"Fake-Va2lue7"
        },
        "json":null,
        "origin":"xxx",
        "url":"https://httpbin.org/post"
    }
}

Expected Behavior

Server should recieve the correct data

{
    "data":{
        "args":{},
        "data":"{\"foo\":\"bar\"}",
        "files":{},
        "form":{},
        "headers":{
            "Accept-Encoding":"gzip",
            "Accept-Language":"it-IT,it;q=0.5",
            "Content-Length":"0",
            "Content-Type":"application/x-www-form-urlencoded",
            "Host":"httpbin.org",
            "User-Agent":"Dalvik/2.1 .0 (Linux; U; Android 11; LEX722 Build/RQ3A.211001.001)",
            "X-Amzn-Trace-Id":"Root=1-xx",
            "X-Fake-Header":"Fake-Va2lue7"
        },
        "json":{"foo":"bar"},
        "origin":"xxx",
        "url":"https://httpbin.org/post"
    }
}

Project Reproduction

https://github.com/LucaFontanot/cap-bug

Additional Information

Tested on two android 11 devices

LucaFontanot avatar Feb 12 '24 16:02 LucaFontanot

I have a similar problem but when using the fetch patch in Capacitor Config

{
  "plugins": {
    "CapacitorHttp": {
      "enabled": true
    }
  }
}

We were uploading images to an AWS bucket using fetch before, but after I enabled CapacitorHttp on the capacitor config, all images where empty, which caused a really big trouble because they where users sensitive data 😢

distante avatar Feb 22 '24 11:02 distante

This issue has been labeled as type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.

ionitron-bot[bot] avatar Feb 26 '24 18:02 ionitron-bot[bot]

I've verified that no data is being appended on iOS or Android if there is no content-type set in the plugin call.

As workaround you can set the content-type in the header by adding "content-type":"application/json" to your headers object.

distante, this issue is only present when using the CapacitorHttp plugin API directly, not in the patched fetch/XHR as the patching code sets the content-type from javascript, so if you are using the patched fetch/XHR and not receiving data, please, create a separate issue with a sample app that reproduces your problem.

jcesarmobile avatar Feb 26 '24 18:02 jcesarmobile

@jcesarmobile I can confirm this was the issue. If no "content-type":"application/json" header is set the application sends no data on post

LucaFontanot avatar Mar 18 '24 11:03 LucaFontanot

that's a workaround, the plugin should send data even if there is no content-type

jcesarmobile avatar Mar 18 '24 13:03 jcesarmobile

I posted an issue here a while ago which points out where the troublesome code is

Was having trouble getting my post requests to send a JSON body, even though my code matched the documentation's demo.

Found out by looking at the @capacitor/core Java code that if ContentType isn't set, it'll just return and not set the body of the request.

https://github.com/ionic-team/capacitor/blob/895f86a81ee76b23ed33fa088ee3c9feec1ecd7f/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java#LL180C46-L180C46

Let me know if you'd prefer to handle something like this a little different. I would've preferred CapacitorHttpUrlConnection.setRequestBody() to throw an Exception, but I figured I'd just try to document the gotcha.

michaelhonan avatar Mar 26 '24 22:03 michaelhonan

Any updates here? We shouldn't need to be using a workaround still

richardcardoso198 avatar Apr 29 '24 19:04 richardcardoso198

I looked at the source of the Capacitor and i'm pretty sure the issue is here https://github.com/ionic-team/capacitor/blob/5b2ab7c68db4e77b8b2961e927acc5a708ee260f/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java#L385

I think a line should be added that checks if no content type is set, but the body is in format of a json, than the content type should be automatically added. I can attempt it but I would like to know if you agree with me first

LucaFontanot avatar May 06 '24 09:05 LucaFontanot

I looked at the source of the Capacitor and i'm pretty sure the issue is here

https://github.com/ionic-team/capacitor/blob/5b2ab7c68db4e77b8b2961e927acc5a708ee260f/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java#L385

I think a line should be added that checks if no content type is set, but the body is in format of a json, than the content type should be automatically added. I can attempt it but I would like to know if you agree with me first

I think the content/type should have a default value, doesn't really matter to check if the body has a JSON format. But i can see why do it the way you said it.

If you want to do it, sure, go ahead, this would help a lot of people.

richardcardoso198 avatar May 29 '24 12:05 richardcardoso198

I looked at the source of the Capacitor and i'm pretty sure the issue is here

https://github.com/ionic-team/capacitor/blob/5b2ab7c68db4e77b8b2961e927acc5a708ee260f/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java#L385

I think a line should be added that checks if no content type is set, but the body is in format of a json, than the content type should be automatically added. I can attempt it but I would like to know if you agree with me first

Nice. Go ahead! 😎🌷

jonas-elias avatar May 29 '24 12:05 jonas-elias