capacitor-community / http

Community plugin for native HTTP
MIT License
209 stars 136 forks source link

iOS and Android handle form-uurlencoded requests differently #270

Open kito99 opened 2 years ago

kito99 commented 2 years ago

Describe the bug iOS and Android handle form-uurlencoded requests differently. I believe the intent is to require the data property to be an object when making a the request, but that's not enforced on the JavaScript side. At the iOS layer, if the data property is a string, it's sent straight though; Android, on the other hand, expects it to be an object, and throws an exception if it's not.

To Reproduce Make a call to Http.request() like so:

const response = await Http.request(
{
  "url":"https://foo.com",
  "method":"POST",
  "headers":{"Accept":"application/json","Content-Type":"application/x-www-form-urlencoded"},
  "data":"a=b&b=c&c=d"
}

Expected behavior The behavior should be the same on both Android and iOS. If the intention is that form requests must have data specified as an object, then both platforms should throw an exception. Otherwise, both should expect a raw string.

Smartphone (please complete the following information):

Smartphone (please complete the following information):

Additional context

Relevant Swift code: https://github.com/capacitor-community/http/blob/3aca516560653349bd6f563b0d6c668e20827e03/ios/Plugin/CapacitorUrlRequest.swift#L100 Relevant Java code: https://github.com/capacitor-community/http/blob/3aca516560653349bd6f563b0d6c668e20827e03/android/src/main/java/com/getcapacitor/plugin/http/CapacitorHttpUrlConnection.java#L193

Clearly the workaround here is to just pass an object, but fixing this would save folks time!

thomaux commented 2 years ago

Thank you so much for reporting this issue! I have been banging my head for the last couple of days on the issue why my oauth2 flow would not complete. It was because the code exchange fails on Android, which is a form-urlencoded POST request.

Fixing the data from a string to an object does the trick. Again, thank you!

kito99 commented 2 years ago

@thomaux you're very welcome! I lost a lot of time with this one, too!