Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Consuming API by sending JSON data format to Third party app using bearer token Authentication by x++ code

(0) ShareShare
ReportReport
Posted on by

HI,

We got an error "The remote server returned an error: (400) Bad Request." while consuming API with third party app. below is my code;

class JSONDeSerialization
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public void consumeAPI()
{

str destinationUrl = 'https:/www.abc.com', responseJSON;
str token = 'xxxxxxxxx';
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
CLRObject clrObj;
System.IO.Stream requestStream, responseStream;
System.IO.StreamReader streamReader;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.IO.Stream stream;
System.IO.Stream dataStream;
//System.Net.NetworkCredential Credentials;

try
{

new InteropPermission(InteropKind::ClrInterop).assert();
httpHeader = new System.Net.WebHeaderCollection();
clrObj = System.Net.WebRequest::Create(destinationUrl);
request = clrObj;
httpHeader.Add("Authorization", strfmt("Bearer %1", token));
request.set_KeepAlive(true);
request.set_ContentType("application/json");
request.set_Method("POST");
request.set_Headers(httpHeader);
response = request.GetResponse();
responseStream = response.getResponseStream();
streamReader = new System.IO.StreamReader(responseStream);
responseJSON = streamReader.ReadToEnd();
info(responseJSON);

//JSON deserialatisation

JSONObjectContract jSONObjectContract = FormJsonSerializer::deserializeObject(classnum(JSONObjectContract), responseJSON);
List listdata = new List(Types::Class);
listdata = jSONObjectContract.parmdata();
ListEnumerator listEnumerator = listdata.getEnumerator();
while(listEnumerator.moveNext())
{
EInvoiceDataContract jsonDataContract = listEnumerator.current();
info(strFmt("%1 %2",jsonDataContract.parmIGSTRate(), jsonDataContract.parmIGSTAmount()));
}

//JSON deserialisation

streamReader.Close();
responseStream.Close();
response.Close();

}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}

}

}

I need help whether I followed correct code or did I missed anything in code?

Need your help with correct code.

Thanks.

  • Community Member Profile Picture
    on at
    RE: Consuming API by sending JSON data format to Third party app using bearer token Authentication by x++ code

    Hi,

    I have using body data in EInvoiceDataContract class from that I'm trying to POST json data. for GET method i got error "the remote server returned an error (405) method not allowed."

    pastedimage1657601479474v1.png

  • Alex VN Profile Picture
    1,994 on at
    RE: Consuming API by sending JSON data format to Third party app using bearer token Authentication by x++ code D365FO

    Hi

    I can see you are using post request but only URL and bearer token provided, seems no body content for the post call. May I know what you are trying to archive?

    Also can you try get instead Post and see?

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,996 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans