web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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?

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,152 Most Valuable Professional on at

    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().

  • Stefan Wojtas Profile Picture
    550 on at

    Hello Martin,

    Thanks for the reply, that was exactly the problem.

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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 681

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 448 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 218 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans