Unexpected End of Document in GraphQL post (taken from example).

Solved

Unexpected End of Document in GraphQL post (taken from example).

BeamJokerFavor
Explorer
51 3 6

I am using the example here:

https://shopify.dev/tutorials/manage-product-media-with-admin-api

I am almost copying and pasting the code, so I don't understand why I am getting an HTTP response"
{"errors":[{"message":"Unexpected end of document","locations":[]}]}

I am using Python.

 

    r = requests.post(url,json=queryparams=params,  headers=headers)
 
query = '''
mutation generateStagedUploads($input: [StagedUploadInput!]!) {
  stagedUploadsCreate(input: $input) {
    stagedTargets {
      url
      resourceUrl
      parameters {
        name
        value
      }
    }
    mediaUserErrors { code, field, message }
  }
}
    '''
params = {
        "input" : [
          {
            "filename""{}.glb".format(name),
            "mimeType""model/gltf-binary",
            "resource""MODEL_3D",
            "fileSize"filesize
          }
        ]
    }
 
 headers= {
        'Content-Type''application/graphql',
        'X-Shopify-Access_Token' :  shopify.StorefrontAccessToken.headers['X-Shopify-Access-Token']
    }
 
Accepted Solution (1)
BeamJokerFavor
Explorer
51 3 6

This is an accepted solution.

I found a post that describes this error with python requests.

The trick is you have to change the 'Content-Type' header to 'application/json'.

Then I had to change the post string to:

    r = requests.post(url,json={"query": query, "variables": params},  headers=headers)

https://github.com/Shopify/graphql-js-client/issues/132

This is how you post to GraphQL using Python request. This may be very helpful to other people.

View solution in original post

Replies 3 (3)

Gregarican
Shopify Partner
1033 86 291

Testing things out using the GraphiQL app, it appears as if the mediaUserErrors property isn't part of the available field set. Here was the error I got back.

{
  "errors": [
    {
      "message": "Field 'mediaUserErrors' doesn't exist on type 'StagedUploadsCreatePayload'",
      "locations": [
        {
          "line": 11,
          "column": 5
        }
      ],
      "path": [
        "mutation generateStagedUploads",
        "stagedUploadsCreate",
        "mediaUserErrors"
      ],
      "extensions": {
        "code": "undefinedField",
        "typeName": "StagedUploadsCreatePayload",
        "fieldName": "mediaUserErrors"
      }
    }
  ]
}

 

When I removed that the GQL request went through just fine. Screen shot below. I'd recommend checking your Python code to ensure that it's properly formatting the GQL request. You can always fire up Fiddler or something to see exactly what's being sent out in the request body.

Untitled.png

Kerryann
Visitor
3 0 0

I don’t understand anything that was sent just now. Please remember that I just closed the account and it was on paused before so I would not be able to go back on it to request a refund 

BeamJokerFavor
Explorer
51 3 6

This is an accepted solution.

I found a post that describes this error with python requests.

The trick is you have to change the 'Content-Type' header to 'application/json'.

Then I had to change the post string to:

    r = requests.post(url,json={"query": query, "variables": params},  headers=headers)

https://github.com/Shopify/graphql-js-client/issues/132

This is how you post to GraphQL using Python request. This may be very helpful to other people.