bug: CapacitorHttp formData file Android "JSValue could not be coerced to JSArray"
Bug Report
Capacitor Version
5.2.1
Platform(s)
Android
Current Behavior
Web: working well, Android: debug the body param is null on android studio.
To native (Capacitor plugin): callbackId: 43900502, pluginId: CapacitorHttp, methodName: post
callback: 43900502, pluginId: CapacitorHttp, methodName: post, methodData: {"url":"https:\/\/domain.com\/","headers":{"Content-Type":"multipart\/form-data"},"dataType":"formData","data":{},"webFetchExtra":{"credentials":"include"}}
JSValue could not be coerced to JSArray.
org.json.JSONException: JSValue could not be coerced to JSArray.
at com.getcapacitor.JSValue.toJSArray(JSValue.java:49)
at com.getcapacitor.plugin.util.CapacitorHttpUrlConnection.setRequestBody(CapacitorHttpUrlConnection.java:215)
at com.getcapacitor.plugin.util.HttpRequestHandler.request(HttpRequestHandler.java:410)
at com.getcapacitor.plugin.CapacitorHttp$1.run(CapacitorHttp.java:34)
at java.lang.Thread.run(Thread.java:1012)
Expected Behavior
Code Reproduction
https://github.com/h3786010/capacitor-http-formdata-issue
Other Technical Details
npm --version 8.19.3
node --version v18.13.0
Additional Context
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.
Adding iOS label too because on iOS I get a DataCloneError: The object can not be cloned on the js side.
Related? https://github.com/ionic-team/capacitor/issues/5945
Any news?
It was documented here that the plugin only supports string or JSON when using the CapacitorHttp.post API directly, complex types have to be serialized to base64 by the users.
Could someone help me how to actually serialize FormData for Capacitor? Native fetch is not an option, I want to try if calling this directly would be able to handle blob response - as others can1t.
You can try what the fetch patch does https://github.com/ionic-team/capacitor/blob/main/core/native-bridge.ts#L33-L52
I still dont know how we can send the File instance directly to the backend, because only part of my app users use android app and rest of them use web-app, i have been trying to do window.fetch, no success there as well. Can anyone give out some sample code where we can successfully send formData to a URL with POST method.?
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.