capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: Capacitor 4.3 Http/Cookies: Unhandled Promise Rejection: DataCloneError: The object can not be cloned.

Open malte94 opened this issue 2 years ago • 13 comments

Capacitor Doctor   

Latest Dependencies:

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

Installed Dependencies:

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

Platform(s)

iOS & Android

Current Behavior

When using Capacitor Http/Cookies on a local build, Mapbox will not load its content anymore. However, this works fine when using the website, or using Capacitor and pointing to the server where the App is hosted on. So it is most likely not related to the implementation.

Possibly this is related to cookies.

Image link: https://user-images.githubusercontent.com/89409894/191803876-cd037583-7a31-4566-b31d-c7a373651b25.png

image

Code


import * as MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
import * as mapboxgl from '!mapbox-gl';

export default function SearchResults(...props) {

let map;

let geocoder = new MapboxGeocoder({
accessToken: "MYKEY",
types: 'place',
countries: 'de',
language: 'de-DE',
placeholder: 'Neue Suche starten',
limit: 5
});

React.useEffect(() => {

  // Load MapBox API

  map = new mapboxgl.Map({
    accessToken: "MYTOKEN",
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    attributionControl: false,
    center: [13.381777, 52.531677],
    zoom: 8
  });

  geocoder.addTo("#geocoder")

  geocoder.on('result', (e) => {
    const result = e.result
    map.setCenter({ lng: result.geometry.coordinates[0], lat: result.geometry.coordinates[1] })
    getUsersByPlace(result)
  });

  window.scrollTo(0, 0);

}, []);

}

Current Behavior

The error must not be thrown and Mapbox must load.

malte94 avatar Sep 22 '22 16:09 malte94

This is most likely related to the http plugin failing to serialize the data being passed from web -> native. The team will be investigating a fix

See: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

ItsChaceD avatar Sep 23 '22 16:09 ItsChaceD

I'm getting the same error when trying to upload a file (FormData).

Example:

const options = {
    url: 'https://example.com/my/api',
    headers: { 'X-Fake-Header': 'Fake-Value' },
    data: FormData,
  };
 
await CapacitorHttp.post(options);

I get the same error even if I set content type header like so: 'Content-Type': 'multipart/form-data'.

brilliantinsane avatar Sep 26 '22 09:09 brilliantinsane

I am getting the same error when I try to download a pdf with window.fetch(pdfurl);

DennisHerr avatar Sep 28 '22 13:09 DennisHerr

I also ran into this error using window.fetch(new URL(...), ...). I had to .toString() on the URL.

brandonaaron avatar Oct 03 '22 20:10 brandonaaron

I also ran into this error while connecting to a remote PouchDB() (https://pouchdb.com/).

Bildschirmfoto 2022-10-17 um 09 34 41

scriptPilot avatar Oct 16 '22 23:10 scriptPilot

I'm getting the same error when trying to upload a file (FormData).

Example:

const options = {
    url: 'https://example.com/my/api',
    headers: { 'X-Fake-Header': 'Fake-Value' },
    data: FormData,
  };
 
await CapacitorHttp.post(options);

I get the same error even if I set content type header like so: 'Content-Type': 'multipart/form-data'.

I'm having the same issue... Did you figure out a work-around for this?

jonesdhtx avatar Oct 17 '22 20:10 jonesdhtx

I am always having the issue with posting FormData, does anyone have a solution?

petergilmour1987 avatar Nov 10 '22 13:11 petergilmour1987

I fixed this by providing the URL as a string, rather than a URL object. (In other words, stick .toString() on the end.)

matthew1000 avatar Dec 15 '22 22:12 matthew1000

Of note, a similar issue happens if you provider headers as a Headers object instead of a plain object. Regular fetch accepts this and I believe this should too. @KevinKelchen so it seems https://github.com/ionic-team/capacitor/issues/6132 and a Headers object both cause the DataCloneError issue.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Headers

silviogutierrez avatar Dec 16 '22 04:12 silviogutierrez

This will be probably solved when PR https://github.com/ionic-team/capacitor/pull/6206 is merged

gpetrov avatar Mar 03 '23 15:03 gpetrov

Hit this with 5.3.0.

[Error] Unhandled Promise Rejection: DataCloneError: The object can not be cloned.
	postMessage
	postToNative (user-script:2:753)
	(anonymous function) (user-script:2:810)
	(anonymous function) (user-script:2:899)
	Promise
	(anonymous function) (user-script:2:898)
	(anonymous function) (user-script:2:439)

as part of OIDC token exchange:

[Log] Object (user-script:2, line 298)

callbackId: "126897699"

methodName: "request"

options: Object

data: URLSearchParams {append: function, delete: function, get: function, getAll: function, has: function, …}

dataType: "json"

headers: Object

Accept: "application/json"

Content-Type: "application/x-www-form-urlencoded"

Object Prototype

method: "POST"

url: "https://login.local.dev/connect/token"

Object Prototype

pluginId: "CapacitorHttp"

type: "message"

Object Prototype

https://github.com/VQComms/oidc-client-ts/blob/main/src/TokenClient.ts#L177

which uses

https://github.com/VQComms/oidc-client-ts/blob/main/src/JsonService.ts#L129

which passes url as a string already

philjones88 avatar Aug 30 '23 11:08 philjones88

Ok it seems the Capacitor Http plugin can't handle URLSearchParams as a body. You have to turn it to a string with a toString() call.

The official docs suggest body can be of this type?

https://developer.mozilla.org/en-US/docs/Web/API/Request/Request

Any body that you want to add to your request: this can be a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), an [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), a [DataView](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView), a [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData), a [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams), a string, or a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) object. Note that a request using the GET or HEAD method cannot have a body.

philjones88 avatar Aug 30 '23 12:08 philjones88

JSON.parse(JSON.stringify(object))

Parsing and objects in formdata through the above solved this problem for me

nbirus avatar Oct 24 '23 14:10 nbirus

can't reproduce on Capacitor 5.7.0, so it was probably fixed when https://github.com/ionic-team/capacitor/pull/6206 got merged as a comment pointed out

jcesarmobile avatar Feb 20 '24 16:02 jcesarmobile

Ok it seems the Capacitor Http plugin can't handle URLSearchParams as a body. You have to turn it to a string with a toString() call.

The official docs suggest body can be of this type?

https://developer.mozilla.org/en-US/docs/Web/API/Request/Request

Any body that you want to add to your request: this can be a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), an [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), a [DataView](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView), a [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData), a [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams), a string, or a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) object. Note that a request using the GET or HEAD method cannot have a body.

@jcesarmobile or everyone coming across this now: The above still seems to be an issue. Meaning with version 5.7.2 I am still getting the DataCloneError when sending an URLSearchParams object as the body with content type application/x-www-form-urlencoded. Passing the parameters via toString() call as suggested works without issue though.

astoiccoder avatar Mar 14 '24 06:03 astoiccoder

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 Apr 13 '24 09:04 ionitron-bot[bot]