Home > Back-end >  Node.js? API Authentication problems
Node.js? API Authentication problems

Time:09-06

This is what my "dev" sent me. Someone help please

I'm trying my best, but their API doesn't respond to our methods. This authentication is the root of the problem. I'm right now using Axios(the most popular and only method for making API requests for web apps) but it's not accepting request

and then i told him i would ask for help*

You can ask this question- How do I make requests for creating order API in my express app? I've tried to make the request by getting my form data from my EJS form using the request.body. But still, it is saying error 400.

Here is his code:

const data = JSON.stringify(req.body);
console.log(data)
const config = {
method: 'post',
url: 'https://labelsupply.io/api/order',
headers: {
'X-Api-Auth': '32854090-03dd-a3c1-Deleted some for safety',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: data
};

axios(config)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
})```

by console.logging we are getting the data, but the API doesn't accepting

The API Docs are here: labelsupply.io/docs
you may need an account to view just put junk

CodePudding user response:

to help you better it would be nice if you could print what error you are getting from axios using console.log(error.message);

Also, what is the structure of your request body?

CodePudding user response:

The API calls for url encoded string.

const data = JSON.stringify(req.body);
console.log(data)
data = new URLSearchParams(Object.entries(data)).toString();
console.log(data); // now should be URL encoded
const config = {
  method: 'post',
  url: 'https://labelsupply.io/api/order',
  headers: {
    'X-Api-Auth': '32854090-03dd-a3c1-Deleted some for safety',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data: data
};

See if the API likes the new encoding?

  • Related