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.
Content-Type: application/json
? the valuejson
might not work for the server. – Davin Tryonapplication/json
. Where do you see it asjson
? – Davin Tryon:json
expands to the valueapplication/json
. Did you at least try it? – Davin Tryon