Search code examples
javascriptnode.jsnpm-request

Nodejs request post with body include api key


I have been trying about a week but I couldn't make a post request to get a result. I tried a bunch of middlewares (exp: 'request', 'axios', 'reqclient','superagent etc..) but I couldn't make it.

Please provide me a simple post request with sending API key and body.

I also read all the documentation.

Please check below to see what I want :

*Authentication API key required.

*O-Auth Scopes trades

*Input One of: user_id + token or user_url is required.


here is my one of try :

const request = require('request-promise')

    const options = {
        method: 'POST',
        uri: 'api-site.com/Offer/v1/',
        headers: {
          'User-Agent': 'Request-Promise',
          'Authorization': 'Basic 123123asdasd123123'
        },
        body: {
            user_url: "site.com/user/user1234123",
            otherparams: "parameter"
          },
          json: true 
      };

      request(options)
      .then(function (response) {
        Console.log(response);
      })
      .catch(function (err) {
        console.log('Error ', err.message);
      });

I am getting this output :

Error  : 401 - {"status":401,"time":1540458426,"message":"API Key Required"}

I tried some other request post middle-wares and played with content-type (application/json. dataForm, x-www-form-urlencoded) or

changed the location of my API key from header to body or

tried my API key inside of auth{authorization: "API Key"}

tried much more.

the result didn't change. I got the same output or errors.

EDIT :

this is the link that I am trying to do but got stack : check here


Solution

  • Solved ! Everything works great. Problem was I needed to send my API Key base64 string.

    Buffer.from("your_api_key_value" + ":", "ascii").toString("base64")