Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Content-Type in the WebRequest cannot be changed.

(0) ShareShare
ReportReport
Posted on by 550

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?

  • Stefan Wojtas Profile Picture
    550 on at
    RE: Content-Type in the WebRequest cannot be changed.

    Hello Martin,

    Thanks for the reply, that was exactly the problem.

    Sometimes it's just a little thing that you overlook...

  • Verified answer
    Martin Dráb Profile Picture
    231,895 Most Valuable Professional on at
    RE: Content-Type in the WebRequest cannot be changed.

    I think the problem is that you set the content type header via set_ContentType(), but then you overwrite all headers by calling set_Headers().

    Try moving set_ContentType() below the call of set_Headers().

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February 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... 293,138 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,895 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans