0
votes

I'm trying to make a POST request to an API but I keep getting a 401 error. I can make GET requests to the same API just fine with 200 status, so I'm wondering if my syntax is off somewhere.

The API I'm using is nocrm.io api and the POST request that I'm trying to make can be found here in their API reference: https://youdontneedacrm.com/api#create-a-lead

Here is my POST request from my server.js file

app.post('/crm/leads', (request, response) => {

    var baseurl = 'https://OURACCOUNT.nocrm.io/api/v2/'
    var apikey = 'API_KEY'
    var path = leads

        axios.post(baseurl + path, {
            params: {
                title: "Testing",
                description: "Business Name: Test Company",
                user_id: "[email protected]",
                tags: ['TAG']
            },
            headers: {'X-API-KEY': apikey, content_type: "json", accept: "application/json"} 
        }).then(function(res){
            console.log("Good");
            response.send(res);
        }).catch(function(error){
            console.log("Error: " + error);
        })
}

This is my request within my client.js file

        $.post('/crm/leads').then(response => {
            console.log(response);
        }).catch(error => {
            console.log("Error: " + error);
        })

The error that I get back from this is: Error: Request failed with 401 status

Here is an example of a GET request that works just fine in server.js file

app.get('/crm/fields', (request, response) => {

    var baseurl = 'https://OURACCOUNT.nocrm.io/api/v2/'
    var apikey = 'API_KEY'
    var path = fields;

        axios.get(baseurl + path, {
            headers: {'X-API-KEY': apikey, content_type: "json", accept: "application/json"},
            params: {'type': "lead"}
        }).then(function(res){
            response.send(res.data);  // send data to client 
        }).catch(function(error){
            console.log("Error: " + error);
        })
}

This is the call to the GET request within my client.js file

$.get('/crm/fields').then(response => {
            console.log(response);
        }).catch(error => {
            console.log("Error: " + error);
        })

Any help or suggestions would be appreciated, thank you!

[EDIT] I found out the problem. My answer is below.

2
Content-Type: application/json? the value json might not work for the server.Davin Tryon
@DavinTryon this is the required header for the API call, which means that's what their server will be expecting.Michael
In the curl command they show as an example, the content-type is application/json. Where do you see it as json?Davin Tryon
@DavinTryon take a look at their ruby example.Michael
I'm pretty sure that ruby :json expands to the value application/json. Did you at least try it?Davin Tryon

2 Answers

1
votes

You can create a general requester to reduce redundancy

function doRequest(method: string, url: string, params?: Object, data?: Object){
    var headers ...;
    return axios({
      method: method,
      baseUrl: 'getfromconfig'
      url: url,
      data: data,
      params: params
    });
}

where data: to post, params: queryparams, method: GET/POST/PUT... the headers can be inited inplace or pass as params. It's reusable in every file.

0
votes

I found the solution to my question while looking at the request logs from the API support. The issue which wasn't clear to me at first, is that I have the headers within the parameters.

The request should look like this instead:

app.post('/crm/leads', (request, response) =>{
    var baseurl = 'https://OURACCOUNT.nocrm.io/api/v2/'
    var apikey = 'API_KEY'
    var path = leads

    var params = {
        title: "Testing",
        description: "Business Name: Test Company",
        user_id: "[email protected]",
        tags: ["TAG"]
    }

     axios.post(baseurl + path,
            params
        ,{
            headers: {'X-API-KEY': apikey, content_type: "json", accept: 
        "application/json"} 
        }).then(function(res){
            console.log("Good");
            response.send(res);
        }).catch(function(error){
            console.log("Error: " + error);
        })
}