简体   繁体   English

使用节点查询GraphQL API

[英]Querying GraphQL APIs with node

currently my code looks like this. 目前我的代码看起来像这样。 The problem is, it is serving me HTML instead of JSON. 问题是,它为我提供HTML而不是JSON。 Am I querying the wrong location? 我在查询错误的位置吗? Should I be using a package more sensible for handling this type of data? 我是否应该使用更适合处理此类数据的包? I just don't understand. 我只是不明白。 Could anyone point me in the right direction? 有人能指出我正确的方向吗? Thanks 谢谢

    var request = require('request');
    var options = {
      url: 'https://api.psychonautwiki.org/?query={  substances {    name    effects {      name    }  }}',
      headers: {
        'Content-Type': 'application/json'
      }
    };

    function callback(error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body);
      }
    }
    request(options,callback);
  }

尝试添加一个Accept标头,告诉服务器你期待什么。

'Accept': 'application/json'

GraphQL apis generally get query and the rest of your options in as the request body, and mostly use the POST http method. GraphQL apis通常将query和其他选项作为请求主体,并且主要使用POST http方法。 It seems like this api reacts to your GET request with a GraphiQL user interface, that's why you are getting html as response. 看起来这个api用GraphiQL用户界面对你的GET请求作出反应,这就是为什么你得到html作为响应。

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

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