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

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

Open
1 of 3 tasks
LucaFontanot opened this issue Feb 12, 2024 · 10 comments · May be fixed by #7490
Open
1 of 3 tasks

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

LucaFontanot opened this issue Feb 12, 2024 · 10 comments · May be fixed by #7490
Labels
type: bug A confirmed bug report

Comments

@LucaFontanot
Copy link

LucaFontanot commented Feb 12, 2024

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
  • 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

@distante
Copy link

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 😢

@jcesarmobile jcesarmobile added the type: bug A confirmed bug report label Feb 26, 2024
Copy link

ionitron-bot bot commented Feb 26, 2024

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.

@jcesarmobile
Copy link
Member

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.

@LucaFontanot
Copy link
Author

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

@jcesarmobile
Copy link
Member

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

@michaelhonan
Copy link

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.

@richardcardoso198
Copy link

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

@LucaFontanot
Copy link
Author

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

public static JSObject request(PluginCall call, String httpMethod, Bridge bridge)

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

@richardcardoso198
Copy link

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

public static JSObject request(PluginCall call, String httpMethod, Bridge bridge)

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.

@jonas-elias
Copy link

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

public static JSObject request(PluginCall call, String httpMethod, Bridge bridge)

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! 😎🌷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A confirmed bug report
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants