7

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.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. Any help would be appreciated. Thanks!

Shooting Stars
  • 755
  • 1
  • 9
  • 20
  • 2
    The docs are straight forward: `{headers:httpheaders}`; but that won't help much because you need to understand request headers first: https://developer.mozilla.org/en-US/docs/Glossary/Request_header – Randy Casburn Nov 18 '18 at 02: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 Nov 18 '18 at 02:46

1 Answers1

1

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 data = await got(url, options).json();
Julien-L
  • 5,267
  • 3
  • 34
  • 51
foma
  • 11
  • 1