Giovanni Cocco - Hi everyone! I have a problem ...
Hi everyone! I have a problem with API Call node. I am trying to call an api with api key authentication. I modified the node but still get an error. In Postman it is working.
Error: Unexpected token '<', "<!doctype "... is not valid JSON
I am calling the POST method with the body in JSON format
{"name":"John Doe","cpfCnpj":"24971563792","email":"john.doe@asaas.com.br","mobilePhone":"4799376637"}
I changed the Authorization header to Access_token, as that is what the API requests.
import fetch from "node-fetch";
export default async function apiCall({
url,
method,
contentType,
Access_token,
body,
shouldAwait,
queryParams
}: NodeInputs, {
logging
}: NodeScriptOptions): NodeOutput {
const headers = {
"Content-Type": contentType
};
if (Access_token) headers["Access_token"] = Access_token;
let queryParamsString = '';
if (queryParams) {
queryParamsString = '?' + new URLSearchParams(queryParams).toString();
}
const fetchOptions = {
method,
headers
};
if (method !== 'GET') {
fetchOptions.body = JSON.stringify(body);
}
const fetchPromise = fetch(url + queryParamsString, fetchOptions);
if (!shouldAwait) {
return {
data: null
};
}
const response = await fetchPromise;
const data = await response.json();
return {
status: response.status,
data
};
}
Any help?5 Replies
I even created a new, simpler node with just the POST method for testing and still got the same error.
import fetch from 'node-fetch';
export default async function callApiPostMethod({
url,
access_token,
name,
cpfCnpj,
email,
mobilePhone
}) {
const requestBody = {
name,
cpfCnpj,
email,
mobilePhone
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access_token': access_token
},
body: JSON.stringify(requestBody)
});
const data = await response.json();
return data;
}
@Giovanni Cocco from this screenshot it shows you have added $ in the access_token, is it required? If the api is open, you can share it's API docs/reference.
@Gaurav Chadha here is the doc https://docs.asaas.com/reference/create-new-customer
Yes the dollar sign is part of the api key. I tried without $ using Postman and I got 401 (Unauthorized).
I tried the same thing on Buildship hoping to get a 401 error and determine that the problem is the dollar sign, but I got the same error as before.
Unexpected token '<', "<!doctype "... is not valid JSON
Asaas - Documentação API
Create new customer
API para desenvolvimento de soluções financeiras para seu sistema.
I got!
Thanks!