noobcoder123
noobcoder123

Reputation: 23

Getting 200 as status code but blank response from api

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);

Upvotes: 2

Views: 5563

Answers (2)

Mohammed Fathi
Mohammed Fathi

Reputation: 1475

I sometime debugging a similar issue only to discover that the path i have in the API handler is different than the one I am calling via ajax ,I am calling this /api/v1/togglePhoneState but in the API handler i am having

module.exports = {
  method: "POST",
  path: "/togglePhoneState/{id}",

  ..etc

}

updating the API handler path to /api/v1/togglePhoneState fixed the issue for me.

Upvotes: 0

Cloyd Abad
Cloyd Abad

Reputation: 737

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);
});

Upvotes: 1

Related Questions