Reputation: 1
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://abc.api/', responseJSON;
str token = 'xxxxxx';
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;
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
httpHeader = new System.Net.WebHeaderCollection();
clrObj = System.Net.WebRequest::Create(destinationUrl);
request = clrObj;
httpHeader.Add("authorization", "Bearer " + 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.
Upvotes: 0
Views: 2208
Reputation: 381
I suspect that since you're POST-ing and setting a content type of "application/json" that you're actually trying to post some json data to the api. But you are not actually adding any content to your request.
This would make sense with the 400 (Bad request) because if the endpoint is expecting some actual json content and you're not sending any it is probably in fact a bad request.
You need to encode the json you want to send into a byte array, probably with ascii, perhaps with utf8, and then write that data to the request's content stream before calling request.GetResponse(). You also probably need to set the content length.
See "Method C" in the answer to this question and you should see the code you're missing: Send HTTP POST request in .NET
And also check out https://learn.microsoft.com/en-us/dynamicsax-2012/developer/net-interop-from-x if you need help translating the C# from that answer into the X++ equivalent statements.
Upvotes: 0