admin管理员组文章数量:1426957
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. I'm using an https request framework known as Got, which is supposed to be a lighter version of request API.
var got = require('got');
var httpheaders = {
'Accept': 'application/json',
'user-key': '**********************',
'json': true
}
got('.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. Any help would be appreciated. Thanks!
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. I'm using an https request framework known as Got, which is supposed to be a lighter version of request API.
var got = require('got');
var httpheaders = {
'Accept': 'application/json',
'user-key': '**********************',
'json': true
}
got('https://developers.zomato./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. Any help would be appreciated. Thanks!
Share Improve this question asked Nov 18, 2018 at 2:19 Shooting StarsShooting Stars 8451 gold badge14 silver badges27 bronze badges 2-
2
The docs are straight forward:
{headers:httpheaders}
; but that won't help much because you need to understand request headers first: developer.mozilla/en-US/docs/Glossary/Request_header – Randy Casburn Commented Nov 18, 2018 at 2:27 - 2 @RandyCasburn Hey thanks for telling me that is the syntax. Funnily enough that was the first thing I tried when I read the documentation. I thought it was wrong but get this, The API call at lat=35, and lon=34 the whole time is just a straight up 404 error. I called it on the API manually to find that out. I just assumed since lat=36 and lon=34 worked, that lat=35 and lon=34 would work, but nope. Thanks for the help. – Shooting Stars Commented Nov 18, 2018 at 2:46
1 Answer
Reset to default 0https://github./sindresorhus/got/blob/HEAD/documentation/2-options.md
You could use options, like this
import got from 'got';
const options = {
headers: {
foo: 'bar'
}
};
const data = await got(url, options).json();
本文标签: javascriptHow to use http request headers in GotStack Overflow
版权声明:本文标题:javascript - How to use http request headers in Got? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745452264a2658941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论