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

Error: Unexpected token P in JSON at position 0 #241

Closed
shriram10567 opened this issue Apr 11, 2021 · 4 comments
Closed

Error: Unexpected token P in JSON at position 0 #241

shriram10567 opened this issue Apr 11, 2021 · 4 comments

Comments

@shriram10567
Copy link

shriram10567 commented Apr 11, 2021

this my apollo client code

const httpLink = new createUploadLink({
    uri: 'http://localhost:4001/graphql',
    credentials: 'include', 
    headers: {
      cookie:
        (typeof window === 'undefined'
          ? ctx?.req?.headers.cookie
          : undefined) || '',
    },
  })

on the node server, i have used upload:false as per apollo docs.

const server = new ApolloServer({
    uploads: false
.....
})

this is what my resolver looks like

  @Mutation(() => Boolean)
  async addPicture(
    @Arg('picture', () => GraphQLUpload)
    { createReadStream, filename }: Upload
  ): Promise<boolean> {
    console.log(filename)
    return new Promise((resolve, reject) =>
      createReadStream()
        .pipe(createWriteStream(__dirname + `../images/${filename}`))
        .on('finish', () => resolve(true))
        .on('error', () => reject(false))
    )
  }

i am getting the following error: Error: Unexpected token P in JSON at position 0.

P.S. before marking this as closed, please look at this stackoverflow post: https://stackoverflow.com/questions/64971480/cant-upload-files-with-apollo-client-graphql-in-next-js-app-post-body-missing. this post explain the problem better than i have done here

@jaydenseric
Copy link
Owner

The unformatted code is too hard to read; please format it properly in an edit and then I'll take a look:

https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting

@jaydenseric
Copy link
Owner

@shriram1056 I edited your code to be syntax highlighted. Please do that yourself next time, as asked. Also, please edit the markdown link URL for the Stack Overflow link as it's currently broken.

Regarding the Error: Unexpected token P in JSON at position 0; most likely the response body is a text message from the server. The message starts with P, so what does it say? You can use the Chrome dev tools network inspector to see the GraphQL response body.

It after that it doesn't become obvious what the problem is, try to figure out if the problem is on the client or server by using the network inspector to see if the outgoing GraphQL request body is a correct GraphQL multipart request.

If it is a client issue, the GitHub issues of this repo is not the right place to ask for help. If it is a server issue, please consult with the Apollo Server community for support. They have a lot of ongoing discussion about their broken support for file uploads in their repos, and Apollo has instructions in their docs for how to get it going.

I'm leaving this issue closed for now, since no evidence of a graphql-upload bug has been presented.

@ru4ert
Copy link

ru4ert commented Jul 31, 2021

The issue is related to the output of this error message, thrown by the Server.
POST body missing, invalid Content-Type, or JSON object has no keys.
For some reason, you have an issue and Apollo is trying to build a JSON object, which don't work for a plain text type response.

I guess your server config is wrong.

@jaymarkjairi
Copy link

jaymarkjairi commented Aug 19, 2021

@shriram1056 I edited your code to be syntax highlighted. Please do that yourself next time, as asked. Also, please edit the markdown link URL for the Stack Overflow link as it's currently broken.

Regarding the Error: Unexpected token P in JSON at position 0; most likely the response body is a text message from the server. The message starts with P, so what does it say? You can use the Chrome dev tools network inspector to see the GraphQL response body.

It after that it doesn't become obvious what the problem is, try to figure out if the problem is on the client or server by using the network inspector to see if the outgoing GraphQL request body is a correct GraphQL multipart request.

If it is a client issue, the GitHub issues of this repo is not the right place to ask for help. If it is a server issue, please consult with the Apollo Server community for support. They have a lot of ongoing discussion about their broken support for file uploads in their repos, and Apollo has instructions in their docs for how to get it going.

I'm leaving this issue closed for now, since no evidence of a graphql-upload bug has been presented.

I sorry to insert it here, since it was related to graphql-upload can you help me about this issue?
https://stackoverflow.com/questions/68850296/apollo-federation-file-upload
because I always see you're comments in other ticket of problem about graphql-upload
thank you so much

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

4 participants