简体   繁体   English

如何在 Got 中使用 http 请求标头?

[英]How to use http request headers in Got?

I have a very simple goal in mind.我有一个非常简单的目标。 I want to make an API request from an API known as Zomato from my node.js server application.我想从我的 node.js 服务器应用程序中从名为 Zomato 的 API 发出 API 请求。 I'm using an https request framework known as Got , which is supposed to be a lighter version of request API.我正在使用称为Got的 https 请求框架,它应该是请求 API 的较轻版本。

var got = require('got');
var httpheaders = {
    'Accept': 'application/json',
    'user-key': '**********************',
    'json': true
}

got('https://developers.zomato.com/api/v2.1/geocode?lat=35&lon=34', {httpheaders}).then(response => {
    console.log('We got something');

    console.log(response.body);

}).catch(error => {
    console.log('We got nothing');
});

When I attempt to run this I catch an error and print, "We got nothing".当我尝试运行它时,我发现一个错误并打印,“我们什么都没有”。 I don't seem to know how to actually include http request headers, but I can't figure out what the proper syntax would be based off the documentation.我似乎不知道如何实际包含 http 请求标头,但我无法根据文档弄清楚正确的语法是什么。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

https://github.com/sindresorhus/got/blob/HEAD/documentation/2-options.md https://github.com/sindresorhus/got/blob/HEAD/documentation/2-options.md

You could use options, like this你可以使用选项,像这样

import got from 'got';从“得到”导入;

const options = { headers: { foo: 'bar' } }; const options = { headers: { foo: 'bar' } };

const data = await got(url, options).json(); const data = await got(url, options).json();

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

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