API Issues with CORS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SPO
    Senior Member
    • Jun 2020
    • 142

    API Issues with CORS

    Dear Espo-Forum,

    I just wanted to inquire whether anyone has tried to use the espo api to insert external data from another service. Specifically, if one has tried to build their own service and use the api to create Leads.

    My current case works as follows: I have created a minor React-App which gathers lead information and sends them via the espo api to my server to create a Lead. However, this does not seem to work as I am always getting the error:

    Failed to load resource: Origin http://xxx.xxx.xxx.xx:3000 is not allowed by Access-Control-Allow-Origin. Status code:200

    Whenever I copy the content of the request (json body) into Postman the request is send and the Lead is created successfully. So I am unsure whether the mistake is on my part or with espo. Nevertheless it seems to me it could be CORS related and my external url is not trusted by the Espo API. I have searched the espo documentation and have not found a point were I could possibly add trusted/allowed origins.

    This is my react code which tries to create a Lead in espo:

    static async postLead(leadData){
    const lead_url = espo_server_url + ConnectionSuffix.LEAD;
    console.log(lead_url);
    console.log("Sending data to espo!");
    console.log(leadData);
    console.log("string:");
    console.log(JSON.stringify(leadData));

    try{
    const response = await fetch(lead_url,
    {
    method : 'POST',
    headers:
    {
    'Access-Control-Allow-Origin': '*',
    'x-api-key': espo_token,
    Accept : 'application/json',
    'Content-Type' : 'application/json',
    },
    body: JSON.stringify(leadData)
    });
    if (!response.ok) {
    console.debug(`Failed with status: ${response.status}`);
    console.debug(response);
    return false;
    }
    return true;
    }
    catch{
    console.debug("Espo Connection Failed!");
    return false;
    }
    }

    Any advice is greatly appreciated! Thank you in advance.
Working...