0

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:

app.post('/order-labels', checkAuthenticated, (req, res) => {
    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.

you may need an account to view just put junk

Shea Hunter Belsky
  • 2,815
  • 3
  • 22
  • 31

1 Answers1

-1

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?

  • {"Type":"4c0980a2-a73b-4bf1-915e-b3371d38a4b3","Weight":"2","FromName":"Ahmadul Kawnine","FromStreet":"7/3/C, Jackson Street","FromCity":"New York","FromZip":"10001","ToName":"Jason","ToStreet":"8/3/C","ToCity":"New York","ToZip":"10002"} – Jason Arendt Sep 05 '22 at 15:07
  • That's JSON. Try setting your 'Content-Type' header to 'application/json' or encoding the data as x-www-form-url-encoded. – David Martin Sep 05 '22 at 17:36
  • Hey i can send you the api docs site just didnt know how to add a file here – Jason Arendt Sep 05 '22 at 18:37
  • I did create a login and get to the API documentation. It does ask for "application/www-x-form-urlencoded" and not "application/json". This answer looks helpful. https://stackoverflow.com/questions/39786337/how-to-convert-js-object-data-to-x-www-form-urlencoded – David Martin Sep 05 '22 at 18:39