Getting 200 as status code but blank response from api

Viewed 933

Im trying to use nodejs request to request data from an api called keywords everywhere, their documentation didnt have anything for nodejs, but they do have bash so what I did was I converted their bash to a nodejs request. for more context, here's their documentation : https://api.keywordseverywhere.com/docs/#/keywords/get_keywords_data My code seems to work because its returning a status code of 200 but the problem is that it returns a blank data:

{
    "data": [],
    "credits": 93735,
    "time": 0
}

If the request is successful it should return something like this:

{
    "data": [
        {
            "vol": 390,
            "cpc": {
                "currency": "$",
                "value": "5.51"
            },
            "keyword": "keywords tool",
            "competition": 0.33,
            "trend": [
                {
                    "month": "May",
                    "year": 2019,
                    "value": 480
                }]
        },
"credits": 95597600,
    "time": 0.02
}

Im guessing there is something wrong with my code since I used a converter and it's not reading the body request properly. Here's my code:

var headers = {
    'Authorization': 'Bearer (API-KEY-HERE)',
    'Accept': 'application/x-www-form-urlencoded'
};


var options = {
    url: 'https://api.keywordseverywhere.com/v1/get_keyword_data',
    method: 'POST',
    headers: headers,
    body: 'dataSource=gkp&country=us&currency=USD&kw[]=keywords&kw[]=keyword'
};

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

request.post(options, callback);
1 Answers

the problem with your code is the body, anyway this is how the code should look like:

const request = require('request');
var options = {
    'method': 'POST',
    'url': 'https://api.keywordseverywhere.com/v1/get_keyword_data',
    'headers': {
    'Authorization': 'Bearer insertAPIKeyHere'
    },
    formData: {
    'kw[]': 'keyword',
    'country': 'us',
    'currency': 'usd',
    'dataSource': 'gkp'
    }
};
request(options, function (error, response) {
    if (error){console.log(error)};
    console.log(response.body);
});
Related