capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

[Bug]: Capacitor HTTP POST application/x-www-form-urlencoded body not encoded on iOS

Open ryaa opened this issue 10 months ago • 2 comments

Capacitor Version

💊 Capacitor Doctor 💊

Latest Dependencies:

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

Installed Dependencies:

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

[success] iOS looking great! 👌 [success] Android looking great! 👌

Other API Details

alexryltsov@Alexs-MacBook-Pro-2 master % npm --version
10.5.0
alexryltsov@Alexs-MacBook-Pro-2 master % node --version
v20.11.1
alexryltsov@Alexs-MacBook-Pro-2 master % pod --version
1.16.2

Platforms Affected

  • [x] iOS
  • [ ] Android
  • [ ] Web

Current Behavior

Capacitor HTTP POST with ContentType application/x-www-form-urlencoded body not encoded on iOS (please note that it is properly encoded on Android so the behavior differs between these platform) - see below

Here is the request on iOS (note that the special characters are not encoded) Image

Here is the same request on Android (note that the special characters are properly encoded) Image

This causes the problem when the server does not recognize the content of the request as expected when sent from iOS platform (all works fine on Android)

It also seems that shouldEncodeUrlParams HttpOption is not supported on iOS (see https://capacitorjs.com/docs/apis/http#httpoptions)

Expected Behavior

Request content/body encoding must be the same for the both iOS and Android platforms.

Project Reproduction

https://github.com/ryaa/capacitor-http-post-application-x-www-form-urlencoded-body-not-encoded-on-ios

Additional Information

See README in the Project Reproduction repository for more details. The potential fix encode before sending the request - see https://github.com/ryaa/capacitor-http-post-application-x-www-form-urlencoded-body-not-encoded-on-ios/blob/master/src/app/home/home.page.ts#L26 or fix Capacitor HTTP plugin - see https://github.com/ionic-team/capacitor/pull/7841 Please note that for the latter fix only % is encoded on iOS (see below) but this fixes the problem on our end and the server correctly recognize the values Image

If shouldEncodeUrlParams HttpOption support is added on iOS (see https://capacitorjs.com/docs/apis/http#httpoptions) this should allow to encode by default and still allow to avoid encoding as it is now (however, this still seems to be breaking change)

ryaa avatar Jan 23 '25 08:01 ryaa

shouldEncodeUrlParams is about url params, not the body.

While it's true that shouldEncodeUrlParams is not implemented for iOS, the url params are being properly encoded on iOS, the problem is on the body encoding.

jcesarmobile avatar Mar 11 '25 14:03 jcesarmobile

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 Mar 11 '25 14:03 ionitron-bot[bot]

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Aug 06 '25 15:08 ionitron-bot[bot]