在Nodejs中通过axios发送数据有问题



当我尝试通过axios发送带有查询数据的数据时。看起来问题出在JSON上。Stringify,它返回不正确的数据,以便能够发送。

这个问题有解决办法吗?

var axios = require('axios');
var data = JSON.stringify({
query: `mutation { flowTriggerReceive(body: "{ "trigger_id": "28a17088-92d7-42e7-a1f2-3594fc4b8bf9", "resources": [ { "name": "AppName", "url": "https://app.doman.com" } ], "properties": { "product_id": 3665695899753, "Rating": 5, "Author": "John Kendy", "Email": "cauhaibg@gmail.com", "Country Code": "VN" } }") { userErrors { field, message } } }`,
variables: {}
});
//console.log(data);
var config = {
method: 'post',
url: 'https://{shop_domain}/admin/api/2023-01/graphql.json',
headers: { 
'X-Shopify-Access-Token': 'xxxxxxxxxxxxxxxxxxx', 
'Content-Type': 'application/json'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});

得到错误

{"errors":[{"message":"Parse error on ": " (STRING) at [1, 51]","locations":[{"line":1,"column":51}]}]}

注意,您不需要将数据对象序列化为JSON,因为Axios将自动为您完成。尝试按原样发送数据对象。不加字符串

var axios = require('axios');
var data = {
query: `mutation { flowTriggerReceive(body: "{ "trigger_id": "28a17088-92d7-42e7-a1f2-3594fc4b8bf9", "resources": [ { "name": "AppName", "url": "https://app.doman.com" } ], "properties": { "product_id": 3665695899753, "Rating": 5, "Author": "John Kendy", "Email": "cauhaibg@gmail.com", "Country Code": "VN" } }") { userErrors { field, message } } }`,
variables: {}
};
var config = {
method: 'post',
url: 'https://{shop_domain}/admin/api/2023-01/graphql.json',
headers: { 
'X-Shopify-Access-Token': 'xxxxxxxxxxxxxxxxxxx', 
'Content-Type': 'application/json'
},
data : data
};

最新更新