简体   繁体   English

Axios、Shopify Storefront API、“参数丢失或无效”、“解析错误”

[英]Axios, Shopify Storefront API, "Parameters missing or invalid", "Parse error"

I am stuck.我被困住了。 I am trying to retrieve data from Shopify's Storefront API.我正在尝试从 Shopify 的 Storefront API 中检索数据。 The post requests work fine in postman.帖子请求在邮递员中工作正常。 They are graphql http requests.它们是 graphql http 请求。 here is a screenshot这是一个截图

So I copied the axios code from the postman app, and pasted it into my React app (which is just react and the shopify buy sdk).所以我从邮递员应用程序中复制了 axios 代码,并将其粘贴到我的 React 应用程序中(这只是反应和 shopify 购买 sdk)。 here is my code:这是我的代码:

var data = JSON.stringify({
    query: `query {     
      shop {
          name
      }
  }`,
    variables: {}
  });

  var config = {
    method: 'post',
    url: 'https://<shop name>.myshopify.com/api/2021-07/graphql.json',
    headers: { 
      'X-Shopify-Storefront-Access-Token': '<access token>', 
      'Content-Type': 'application/json'
    },
    data : data
  };

  axios(config)
  .then(function (response) {
    console.log(JSON.stringify(response.data));
  })
  .catch(function (error) {
    console.log(error.response);
  });

And this is the error it returns: status 400, "parameter missing or invalid".是它返回的错误:状态 400,“参数丢失或无效”。 So I tried something like this instead:所以我尝试了这样的事情:

const url = 'https://<shop-name>.myshopify.com/api/2021-07/graphql.json'
  const headers = {
    'X-Shopify-Storefront-Access-Token': '<access token>',
    'Content-Type': 'application/json',
  }

await axios.post(
    url, // this is the same as the previous url
    {query: `
      shop {
        name
      }
    `},
    {headers: headers}) // headers are the same as previous headers
  .then((result) => console.log(result))
  .catch((error) => {
      console.log(error.response);
  });

and I now get a status 200, but with no store name.我现在得到一个状态 200,但没有商店名称。 Rather, I get a parse error : "Parse error on "shop" (IDENTIFIER) at [2, 11]".相反,我收到一个解析错误:“[2, 11] 处的“商店”(IDENTIFIER)上的解析错误”。 And if I change my 'Content-Type' header to 'application/graphql', I get a similar parse error .如果我将“Content-Type”标头更改为“application/graphql”,我会收到类似的解析错误

Please help.请帮忙。 I have no idea how to get this to work.我不知道如何让它工作。 Thanks in advance.提前致谢。

First you must understand the error codes from graphql.首先,您必须了解来自 graphql 的错误代码。 The graphql endpoint always returns 200 when the POST was successfull, but there can be still a nested error by resolving your request.POST成功时,graphql 端点总是返回 200,但通过解析您的请求仍然可能存在嵌套错误。

Second, your query with TypedStringArray misses a query .其次,您使用TypedStringArray的查询错过了一个query It should look like它应该看起来像

const url = 'https://<shop-name>.myshopify.com/api/2021-07/graphql.json'
  const headers = {
    'X-Shopify-Storefront-Access-Token': '<access token>',
    'Content-Type': 'application/json',
  }

await axios.post(
    url, // this is the same as the previous url
    {data : {
       query: `
         query GiveMeMyShop {
           shop {
             name
         }
       }
    `},
    {headers: headers}) // headers are the same as previous headers
  .then((result) => console.log(result))
  .catch((error) => {
      console.log(error.response);
  });

for the correct query syntax.获取正确的查询语法。 But in all cases i recommend to use a gql client like apollo.但在所有情况下,我都建议使用像 apollo 这样的 gql 客户端。

Got same issue and find the solution.遇到同样的问题并找到解决方案。 The data should be an object {query:"xxxx"};数据应该是一个对象 {query:"xxxx"}; For GraphQL.对于 GraphQL。


  var config = {
    method: 'post',
    url: 'https://<shop name>.myshopify.com/api/2021-07/graphql.json',
    headers: { 
      'X-Shopify-Storefront-Access-Token': '<access token>', 
      'Content-Type': 'application/json'
    },
     data: {
        query: `query {
            shop {
                name
              }
          }`,
      },
  };

response:回复:

     method: 'post',
     url: 'https://qxxxx.myshopify.com/api/2022-04/graphql.json',
     data: '{"query":"query {\\n            shop {\\n                name\\n              }\\n          }"}'
>    },

Shopify graphQL Postman screenshot: Shopify graphQL Postman 截图: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM