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

File sent as [object Object] in request payload #96

Closed
howdyjessie opened this issue Jun 22, 2018 · 9 comments
Closed

File sent as [object Object] in request payload #96

howdyjessie opened this issue Jun 22, 2018 · 9 comments

Comments

@howdyjessie
Copy link

howdyjessie commented Jun 22, 2018

Hi, I'm trying to upload a file on React Native using ReactNativeFile like this:

const SET_PIC = gql`
  mutation setUserPicture($userId: ID!, $picture: Upload!) {
    setUserPicture(userId: $userId, picture: $picture)
  }
`;

const file = new ReactNativeFile({
      uri: 'path/to/file',
      type: 'image/jpeg',
      name: 'photo.jpg',
    });


this.props.setUserPicture(file).then(() => {
      // do things after picture upload
    });

graphql(SET_PIC, {
    props: ({ mutate }) => ({
      setUserPicture: picture => mutate({
        variables: {
          picture,
        },
      }),
    }),
  })

but in my request payload, the file gets set as [object Object], like:

------WebKitFormBoundaryWczBfJPpAvc3HDSr
Content-Disposition: form-data; name="operations"

{"operationName":"setUserPicture","variables":{"userId":"123","picture":null},"query":"mutation setUserPicture($userId: ID!, $picture: Upload!) {\n  setUserPicture(userId: $userId, picture: $picture)\n}\n"}
------WebKitFormBoundaryWczBfJPpAvc3HDSr
Content-Disposition: form-data; name="map"

{"0":["variables.picture"]}
------WebKitFormBoundaryWczBfJPpAvc3HDSr
Content-Disposition: form-data; name="0"; filename="photo.jpg"
Content-Type: image/jpeg

[object Object]
------WebKitFormBoundaryWczBfJPpAvc3HDSr--

Am I using ReactNativeFile incorrectly here? Why do I get [object Object]?

@jaydenseric
Copy link
Owner

Strange. Maybe your fetch and FormData polyfills are not suitable?

@howdyjessie
Copy link
Author

Hmm I'm using the fetch polyfill that comes with react-native. Is it necessary to provide a separate FormData polyfill as well? How should I make sure my polyfills are suitable?

@jaydenseric
Copy link
Owner

I don't know! I don't use React Native. Maybe @giautm can help?

@giautm
Copy link
Contributor

giautm commented Jun 26, 2018

Did you check the content of your files? It looks like it really has [object Object] in the content. 😄

@howdyjessie
Copy link
Author

Alas, it is an actual image! 😅

@madyankin
Copy link

madyankin commented Jul 21, 2018

If you use React Native Debugger, you have to know that it replaces RN's fetch with the one bundled inside Electron. The Electron's (Chrome's) fetch knows nothing about how to handle React Native's files. So, you should detach your app from the debugger to use RN's fetch. The only way to debug file submissions is to watch the requests at the server side.

jhen0409/react-native-debugger#38 (comment)

@jaydenseric
Copy link
Owner

For now there is not much I can do to help; if more information comes to light we can investigate further and reopen 🙏

@TeChn4K
Copy link

TeChn4K commented Jun 7, 2019

If you use React Native Debugger, you have to know that it replaces RN's fetch with the one bundled inside Electron. The Electron's (Chrome's) fetch knows nothing about how to handle React Native's files. [...]

OMG @Outpunk thank you so much! One day to understand why all my upload requests were failing ..!

@jamesone
Copy link

@madyankin LIFE SAVER! @jaydenseric I will open a PR to document this

jamesone added a commit to jamesone/apollo-upload-client that referenced this issue Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants