我正在尝试向Githubs graphql api(v4)发送http请求。我的猜测是我的查询格式是错误的。以下代码用于向api发送POST请求。
app.get('/fetch-data', function(req, res, next) {
const access_token = settings.dev.TOKEN;
const query = {query: { viewer: { 'login': 'muckbuck' } }};
const options = {
uri: 'https://api.github.com/graphql',
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': "Bearer " + access_token,
'User-Agent': 'request',
'contentType': "application/graphql",
},
data: query
};
function callback(error, response, body) {
console.log(response.body)
if (!error && response.statusCode == 200) {
console.log(body);
console.log('lol');
}
}
request(options, callback);
});
我得到的错误消息是:
{"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/v3"}
发布于 2017-08-09 00:10:08
我认为您的GraphQL是错误的,因为query
密钥应该包含一个字符串,而不是一个复杂的结构。另请参阅GitHub应用编程接口文档中的this example。
您可能还希望按照GraphQL introduction的建议将Content-Type
设置为application/json
。application/graphql
似乎不是registered media type,似乎是GraphQL的alter the behaviour。
https://stackoverflow.com/questions/45566784
复制相似问题