Hello dear community,
I am currently in the process of programming a WebRequest from AX2012.
The requirement is that I pass the Content-Type as "application/json;charset=UTF-8.
Unfortunately it seems that AX ignores the .set_ContentType.
We wrote a small test service, which as an answer shows me what the original request was.
Here are some code examples.
private void requestService() { System.String response = ""; System.Net.HttpWebRequest httpRequest = null; System.Net.HttpWebResponse httpResponse = null; System.Net.WebHeaderCollection httpHeader = null; CLRObject clro = null; System.IO.Stream stream = null; System.IO.StreamReader streamReader = null; System.Byte[] bytes = null; System.Byte[] byteArray = null; System.Text.Encoding encoding = null; boolean ret = true; str byteStr; byteStr = strfmt("%1:%2", rampParameter.Username, rampParameter.Password); try { new InteropPermission(InteropKind::ClrInterop).assert(); httpHeader = new System.Net.WebHeaderCollection(); clro = System.Net.WebRequest::Create(this.getRequestUrl()); httpRequest = clro; httpRequest.set_Method("POST"); httpRequest.set_ContentType(@"application/json;charset=UTF-8"); encoding = System.Text.Encoding::get_UTF8(); byteArray = encoding.GetBytes(byteStr); byteStr = System.Convert::ToBase64String(byteArray); httpHeader.Add("Authorization", 'Basic ' byteStr); httpRequest.set_Headers(httpHeader); bytes = encoding.GetBytes(this.buildJsonString()); httpRequest.set_ContentLength(bytes.get_Length()); stream = httpRequest.GetRequestStream(); stream.Write(bytes,0,bytes.get_Length()); stream.Close(); httpResponse = httpRequest.GetResponse(); stream = httpResponse.GetResponseStream(); streamReader = new System.IO.StreamReader(stream); response = streamReader.ReadToEnd(); CodeAccessPermission::revertAssert(); } catch(Exception::CLRError) { ret = false; throw error(AifUtil::getClrErrorMessage()); } }
In the example, in line 26, I set the ContentType via the request method.
But that doesn't seem to work.
As a result I get the response:
{"Headers":{"Authorization":["Basic XXXX","Connection":["Keep-Alive","Content-Length":["65","Expect":["100-continue ","Host":["iis2016-01:4747"},"Query":{"value":"","hasValue":false},"RouteValues":{"action":"Post", "controller":"Values"},"Body":"{\"gate\":\"10\",\"direction\":\"UNLOAD\",\"start\":\"2022-09 -08T12:14:14Z\"}"}
We tested the service that we programmed, if we send a request via Postman, we get the corresponding content type back.
We then tried, instead of
httpRequest.set_ContentType(@"application/json;charset=UTF-8");
to use the following code and put the type directly in the header:
httpHeader.Add("content-type",@"application/json;charset=UTF-8");
But then we get the following Infolog error message:
"The 'content-type' header must be modified with the appropriate property or method.
Parameter name: name"
Do any of you see the error in the code? We're currently a bit clueless.
Or is that perhaps a mistake by AX?