Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formdata sends [object object] in request #38

Closed
sagark1510 opened this issue Jan 20, 2017 · 48 comments
Closed

Formdata sends [object object] in request #38

sagark1510 opened this issue Jan 20, 2017 · 48 comments
Labels

Comments

@sagark1510
Copy link

sagark1510 commented Jan 20, 2017

When I try to call POST and request's content type is multipart/formdata it shows [object object] in request params and API can not get any properties. It works good with JSON data.

Does anyone else having same problem?

@jhen0409
Copy link
Owner

If you're followed this section, for the multipart/formdata request, you need use FormData of debugger worker instead of FormData of React Native:

global.FormData = global.originalFormData

I'll add it to readme, thanks for issue!

@sagark1510
Copy link
Author

It worked. Awesome. Thanks @jhen0409 :)

@azzgo
Copy link

azzgo commented Jul 1, 2017

I don't understand, I debugged in to RNDebuggerWorkder.js, I see you override FormData use originalFormData in your worker/devMenu.js file. So is there a example code show how to upload formdata file when connect debugger window?

@jhen0409
Copy link
Owner

jhen0409 commented Jul 1, 2017

I see you override FormData use originalFormData in your worker/devMenu.js file.

It override only if you're enabled Network Inspect.

So is there a example code show how to upload formdata file when connect debugger window?

It doesn't support RN implemented uri (from CameraRoll) for FormData, please disable Network Inspect if you're use the feature for upload.

Also, I personally use react-native-fetch-blob for upload file.

@azzgo
Copy link

azzgo commented Jul 1, 2017

cool, thanks, I will try it

@tmaly1980
Copy link

@jhen0409 Unfortunately, that link doesn't explain anything about this issue. I can't find any details in the documentation as to what to do. Am I supposed to use global.originalFormData in my app? Will I be unable to track the request/response in my debugger? I'm somewhat reliant on that to ensure that my file uploads are working.

@jhen0409
Copy link
Owner

jhen0409 commented Aug 31, 2017

@tmaly1980 I've been updated the documentation, you can found in debugger-integration.md, it explained the limitations.

@tmaly1980
Copy link

tmaly1980 commented Aug 31, 2017

@jhen0409 OK, but what is the solution? This is my code (using apisauce/axios):

  const saveProfilePhoto = (file) => {
    let filename = file.uri
    let fileUrl = (!filename.match(/^file:/) ? 'file://' : '') + filename
    let fileMeta = {
      uri: fileUrl,
      type: mime.lookup(fileUrl),
      name: fileUrl.split(/[\\/]/).pop() // basename
    }
    const form = new FormData()
    form.append('avatar', fileMeta)

    return api.post('user/profile_photo', form)
  }

This is my request payload (as seen in the network inspector, but my server end also sees this same problem):

------WebKitFormBoundary4fbYRJMz2PnpXyj4
Content-Disposition: form-data; name="avatar"

[object Object]
------WebKitFormBoundary4fbYRJMz2PnpXyj4--

I've also tried fetch() with the same result. Whichever FormData is being used is turning the file content itself into a string. Why doesn't it do what it's supposed to? And how do I fix this problem? I've tried using global.originalFormData but it makes no difference.

@jhen0409
Copy link
Owner

jhen0409 commented Aug 31, 2017

@tmaly1980 please don't enable this feature (Network Inspect) for uri of FormData, debugger worker's XHR doesn't support it, as I wrote in documentation:

React Native FormData support uri property you can use file from CameraRoll, but originalFormData doesn't supported.

@tmaly1980
Copy link

tmaly1980 commented Aug 31, 2017

@jhen0409 how do I disable it? This just works by default out of the box. Through the touch bar (simulator in my case)?

@jhen0409
Copy link
Owner

I'm sure it's disabled by default, you can check context menu of RNDebugger (Enable / Disable Network Inspect), it will print [RNDebugger] Network Inspect is enabled... in console log if enabled. Otherwise you can tracking your app code that have replace XMLHttpRequest with originalXMLHttpRequest.

@tmaly1980
Copy link

I'm actually not overwriting XMLHttpRequest and FormData, I believe it's the Touch Bar simulator (mentioned https://github.com/jhen0409/react-native-debugger/blob/master/docs/debugger-integration.md#how-network-inspect-works) that is enabling/disabling the network inspect.

@jhen0409
Copy link
Owner

jhen0409 commented Aug 31, 2017

It should false by default, I don't sure if you have inadvertently pressed it (context menu or touch bar), it will persisted in local storage, even you can check localStorage.networkInspect in console (top context). Just toggle it should works.

I might consider to removing the localStorage value, so it will reset by re-open window.

@tmaly1980
Copy link

I've disabled Network Inspect and now all my network requests are timing out. I don't recall overwriting XMLHttpRequest, I suspect there's a package that is doing that for me (which one, I'm not sure). I'm not sure how to restore the original XMLHttpRequest.

@jhen0409
Copy link
Owner

I've disabled Network Inspect and now all my network requests are timing out.

It shouldn't related to original XMLHttpRequest issue if the situation changed, you may need to check the native requests. If you're on iOS, I'm not sure if it blocked the domain name (you have to set NSExceptionDomains), but it shouldn't be timeout problem. :\

There is a way to check if you using original XMLHttpRequest or not (RNDebuggerWorker.js context):

2017-09-01 2 33 02

function XMLHttpRequest() { [native code] } is meaning you're using the original XMLHttpRequest.

@captain-xu
Copy link

@tmaly1980 hi if you set global.XMLHttpRequest = global.originalXMLHttpRequest ? global.originalXMLHttpRequest : global.originalXMLHttpRequest; global.FormData = global.originalFormData ? global.originalFormData : global.FormData; in your code, please disable it

@juanpasolano
Copy link

I'm having the same issue where

------WebKitFormBoundary4fbYRJMz2PnpXyj4
Content-Disposition: form-data; name="file"

[object Object]
------WebKitFormBoundary4fbYRJMz2PnpXyj4--

And none of the solutions above has worked for me :(
I'm on "expo": "^28.0.0",

@banli17
Copy link

banli17 commented Jul 13, 2018

@juanpasolano

maybe you open below config one,remove all。

GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest
global.FormData = global.originalFormData

@yasserzubair
Copy link

Any solution to it yet? I have tried everything and i'm still getting

------WebKitFormBoundary4fbYRJMz2PnpXyj4
Content-Disposition: form-data; name="file"

[object Object]
------WebKitFormBoundary4fbYRJMz2PnpXyj4--

@banli17
Copy link

banli17 commented Jul 31, 2018

@yasserzubair
close js debugger it's ok, dont know why

@shide1989
Copy link

Still having the issue on React Native 0.55.2

@shide1989
Copy link

On React Native 0.55.2, using the following code :

let options = {
  method: 'POST',
  headers: {
  'Accept': 'application/json'
    }
  }
let form = new FormData()
form.append(key, {
  uri: value.uri,
  type: 'image/jpeg',
  name: 'avatar.jpg'
  })
options.body = form
let response = await fetch(config.api.host + path, options)

on the server side, i get the following info (JS express) :

req.header.content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryBcWJX9dEonAwAFvD'
req.body.avatar: '[object Object]'
req.file : undefined
req.files: []

any help ?

@Graig123git
Copy link

was this issue ever resolved ?

@TBGerstenberg
Copy link

was this issue ever resolved ?

.... was it?

@shide1989
Copy link

To me this was never fixed

@TBGerstenberg
Copy link

Did you manage to get file uploads working somehow diffently? Im using multer on the backend, which expects the data to come in as type "mutipart-form". I'm getting the same behaviour of sending [object object] when network inspect is enabled, breaking (all) other requests when disabling it. Any idea why the latter is happening?

Logging the "XMLHttpRequest" Object is giving me the before mentioned "function XMLHttpRequest() { [native code] }" response, which means (following the above answer from @jhen0409 ) that the Debugger is not overriding it.

Kind regards

@shide1989
Copy link

@tobige we managed to upload files directly in the POST data, using base64 strings. This wasn't the best practice, but it worked

@TBGerstenberg
Copy link

Thanks for replying @shide1989 , we are currently still using a formdata-request, which is working when network-inspect is disabled on the debugger and the emulator is pointed to the IP of the Node running the backend, instead of using localhost:. Since this is rather hard to debug, we may switch to using base64-encoded images just for the sake of re-enabling a sound debugging experience.

Cheers

@kyle-ssg
Copy link

This issue really impacts the ability not only to upload images whilst debugging but actually use the debugger to verify the payload that is being sent.

global.FormData = global.originalFormData does work but it removes every single network request from the debugger, completely breaking the network inspect.

@zewish
Copy link

zewish commented Jan 27, 2020

Is there any way this can be fixed? Or is it a limitation of React Native itself that’s too big to overcome, @jhen0409? I’d be glad to contribute to the project if this is fixable somehow.

@MakhouT
Copy link

MakhouT commented Feb 3, 2020

I'm having the exact same issue. Is there an other way to debug this?

@DirKraft91
Copy link

how fix it?

@shide1989
Copy link

It's been a while since I had this issue, but on other Non-RN projects I remember that removing the "Content-Type" header lead to the XMLHttpRequest to interpret itself the body and put the right headers & content, maybe this can be a lead

@DhawanRachakonda
Copy link

It's been a while since I had this issue, but on other Non-RN projects I remember that removing the "Content-Type" header lead to the XMLHttpRequest to interpret itself the body and put the right headers & content, maybe this can be a lead

You need to update FLIPPER_VERSION of gradle.properties to FLIPPER_VERSION=0.39.0. Afetr that it worked for me.
After you update you have to clean gradle, else it wont work. Here are links which found me helpful:
https://stackoverflow.com/questions/61046007/form-post-with-file-attach-throws-network-error-react-native-react-native-im
facebook/flipper#993 (comment)

@devethan
Copy link

If you debugging your project with react-native-debugger, FormData().append is not work as you intended.
The appended data will be delivered as a string.
Screen Shot 2020-12-18 at 3 48 46 PM

Quit debugging mode right now, and try it❗️

@devchris
Copy link

This has caused me hours of debugging...

@banjo09
Copy link

banjo09 commented May 26, 2021

Doing this:

appending the uri information does not work. It doesn't even send the network request when I try to append an object. But if I append it as a string using Json stringify method.

Then it will send the request, but it breaks for obvious reasons. Why is FormData not allowing me to append an object?

from react-native-image-picker/react-native-image-picker#798 by
https://github.com/reboss

@vcavallo
Copy link

What is going on with this? Why is this issue closed if it's still causing problems for people three years later?

@1Jesper1
Copy link

Also have this problem.

@mohity777
Copy link

If react native version is 0.62+ then use flipper for debugging instead. It provide network inspect too with a lot of other cool stuff.

@pandreyk
Copy link

If you use 'multipart/form-data' remove 'Content-Type': 'multipart/form-data' from headers

@HarikaranKananathan
Copy link

HarikaranKananathan commented Jan 13, 2022

Also have this problem.
2
image

@nomanoff
Copy link

global.FormData = global.originalFormData

Where do I paste this code? Can anybody explain this to me?

@nomanoff
Copy link

global.FormData = global.originalFormData

Where do I paste this code? Can anybody explain this to me?

React native debugger is working fine. But if network ispection is on, my image requests look like [Object object] but if I turn off network inspect everything seems to be fine. But I need to debug sometimes, so I can't do it right now.

@orangec-at
Copy link

never solved?

@timotgl
Copy link

timotgl commented Nov 28, 2022

This still remains an issue apparently. Turning on "Inspect network requests" can essentially corrupt your requests, which is quite unexpected. https://github.com/jhen0409/react-native-debugger/blob/master/docs/debugger-integration.md#how-network-inspect-works doesn't explain any of this.

@Stephane226
Copy link

WHERE TO PAST THIS CODE ? can you guys give info about that please ?

@arvindyadav2
Copy link

this issue never get resolved tried everything no solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests