Skip to main content

Notifications

Announcements

No record found.

Customer Insights - Journeys forum
Unanswered

400 Error when calling Events API from an Azure function

Posted on by

I am trying to call the Events API from an Azure function using node.js

I have created the web application by following 

https://learn.microsoft.com/en-us/dynamics365/marketing/developer/register-web-application-events-api

I got the api endpoint and token.

I also registered my Azure function url as the origin when creating the web application.

For example if my function app url is

FnApp Url

https://msazfunctionapp.azurewebsites.net/api/msazfunctionname?code=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=

In the origin field I registered 

https://msazfunctionapp.azurewebsites.net/

After creating the web application , I followed below link to use Events API in an Azure function

https://learn.microsoft.com/en-us/dynamics365/marketing/developer/using-events-api

But when I run the function from azure portal using Code Test tab , I see the below error in response 

Response status : 400
2023-06-04T06:40:50Z [Information Error in Events Api : SyntaxError: Unexpected end of JSON input
Here is the node.js code I used in azurefunction to call the Events API
const https = require('https');





module.exports = async function (context, req) {



    try {

        context.log('JavaScript HTTP trigger function for D365 Events API processed a request.');



        

        

        try {

        const requestOptions = {

        method : 'GET',

        headers : { 'Content-Type': 'application/json' }

        };

     



        const fetchUrl = 'https://xxxxxxxxxxxxxxxxxxx.svc.dynamics.com/EvtMgmt/api/v2.0/events/published?emApplicationtoken=xxxxx-xxxxx-xxxxxxxxx’;

        const response = await fetch(fetchUrl);

        

        context.log('Response status : '    response.status);

        

        const respData = await response.json();

        const JSONrespData = JSON.stringify(respData);

        

        context.log('Response data as string : '   respData);

        context.log('Response data as Object : ' , respData);

        context.log('Response data JSON Stringified : '   JSONrespData);

        } catch(err) {

            context.log('Error in Events Api : '   err);

        }



       // context.res = data;

        const responseJSON = {

            "data" : respData

        }

        context.res.headers = { "Content-Type": "application/json" };

        context.res.headers = { "content-type": "application/json" };

        context.res.body = responseJSON;

        

        context.done();



        

    } catch(err) {

        context.res = {

            status: 500

        };

    }

}
The documentation to use Events API mentions that "If you do not provide a web application token or the Origin Header, the Events API will return HTTP 400 Bad Request."
I have provided the token but I don't think we can manually specify the Origin header

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,252 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,089 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans