Giovanni CoccoG
BuildShip17mo ago
5 replies
Giovanni Cocco

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?
Was this page helpful?