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
Add a ment  | 

1 Answer 1

Reset to default 0

https://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