capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: CapacitorHttp formData file Android "JSValue could not be coerced to JSArray"

Open h3786010 opened this issue 2 years ago • 4 comments

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

h3786010 avatar Jul 17 '23 03:07 h3786010

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 Jul 18 '23 15:07 ionitron-bot[bot]

Adding iOS label too because on iOS I get a DataCloneError: The object can not be cloned on the js side.

jcesarmobile avatar Jul 18 '23 15:07 jcesarmobile

Related? https://github.com/ionic-team/capacitor/issues/5945

philjones88 avatar Sep 05 '23 14:09 philjones88

Any news?

FrancescoPaiola avatar Jan 17 '24 14:01 FrancescoPaiola

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.

jcesarmobile avatar Mar 15 '24 15:03 jcesarmobile

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.

IRadics avatar Mar 21 '24 19:03 IRadics

You can try what the fetch patch does https://github.com/ionic-team/capacitor/blob/main/core/native-bridge.ts#L33-L52

jcesarmobile avatar Mar 22 '24 08:03 jcesarmobile

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.?

suhasdinesh avatar Apr 05 '24 15:04 suhasdinesh

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 May 05 '24 15:05 ionitron-bot[bot]