Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

400 Error when calling Events API from an Azure function

(0) ShareShare
ReportReport
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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans