[Bug]: Android POST and PUT requests are not sending any data
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
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 😢
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.
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 I can confirm this was the issue. If no "content-type":"application/json" header is set the application sends no data on post
that's a workaround, the plugin should send data even if there is no content-type
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.
Any updates here? We shouldn't need to be using a workaround still
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 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.
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! 😎🌷