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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Misused header name, 'Content-Type'. Make sure request headers are used with HttpRequestMessage,

(3) ShareShare
ReportReport
Posted on by 101
I have the following error. please help me with Corrected text. I am ok in AL coding but not in JSON, or HTTP
----
Job finished executing.
Status: Error
Error: Misused header name, 'Content-Type'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
 
-----------------
local procedure GetAuthToken(var Token: Text): Boolean
    var
        Client: HttpClient;
        Request: HttpRequestMessage;
        Response: HttpResponseMessage;
        JsonResponse: JsonObject;
        Content: Text;
        Headers: HttpHeaders;
        BearerToken: JsonToken;
    begin
        Request.SetRequestUri('http://xxx.xxx.xxx.x:8080/elicensing/token/auth-bearer');
        Request.Method := 'POST';
 
        Request.GetHeaders(Headers);
        Headers.Add('Content-Type', 'application/json');
        Request.Content().WriteFrom('{"username": "xxxx", "password": "55555"}');
 
        Client.Send(Request, Response);
        if not Response.IsSuccessStatusCode() then
            exit(false);
 
        Response.Content().ReadAs(Content);
        JsonResponse.ReadFrom(Content);
        if JsonResponse.Contains('bearerToken') then begin
            JsonResponse.Get('bearerToken', BearerToken);
            Token := BearerToken.AsValue().AsText();
        end else
            Token := '';
        exit(Token <> '');
    end;
I have the same question (0)
  • Suggested answer
    Sohail Ahmed Profile Picture
    11,150 Super User 2025 Season 2 on at

    Your code has a few mistakes related to how headers are handled.

     

    Specifically, Request.GetHeaders() should be replaced with HttpContent.GetHeaders() when you're working with content-specific headers like Content-Type.

     

    To help you understand better, I’ve attached a sample code snippet below that shows how to structure the request properly using HttpRequestMessage, HttpContent, and headers.

     

     

    Let me know if you need help adjusting it to your use case.

     

    Mark below checkbox to make this answer Verified if it helps you.

  • Suggested answer
    Sohail Ahmed Profile Picture
    11,150 Super User 2025 Season 2 on at

    I just noticed you're trying to generate an access token, so for your reference, I’m attaching my Access Token generation code as well.

    Also, I believe a few parts of your code can be improved:


    • Avoid hardcoding the username and password directly in the code.

    • Instead of passing a hardcoded JSON string as text, consider using AL’s built-in AddJson() methods to build your request body more safely and cleanly.

     

    procedure AccessToken()
        var
            lRecSetup: Record "Seller Cloud Setup";
            HttpClient: HttpClient;
            HttpContent: HttpContent;
            HttpHeadersContent: HttpHeaders;
            HttpRequestMessage: HttpRequestMessage;
            HttpResponseMessage: HttpResponseMessage;
            JsonObject: JsonObject;
            _URL: Text;
            _RequestBodyContent: Text;
            _Response: Text;
    
            TokenResponseHandler: Codeunit "Token Response Handler";
        begin
            Clear(_URL);
            Clear(_RequestBodyContent);
            Clear(_Response);
            Clear(JsonObject);
    
            lRecSetup.FindFirst();
            _URL := lRecSetup."Token URL";
    
            HttpRequestMessage.Method('POST');
            HttpRequestMessage.SetRequestUri(_URL);
    
            JsonObject.Add('Username', lRecSetup."User Name");
            JsonObject.Add('Password', lRecSetup.Password);
    
            JsonObject.WriteTo(_RequestBodyContent);
            HttpContent.WriteFrom(_RequestBodyContent);
            HttpContent.GetHeaders(HttpHeadersContent);
            HttpHeadersContent.Clear();
            HttpHeadersContent.Add('Content-Type', 'application/json');
            HttpRequestMessage.Content := HttpContent;
            HttpClient.Post(_URL, HttpContent, HttpResponseMessage);
            if (HttpResponseMessage.IsSuccessStatusCode) then begin
                HttpResponseMessage.Content.ReadAs(_Response);
                TokenResponseHandler.StoreTokenResponse(_Response);
            end;
    
        end;
    


       

    Let me know if you need help refactoring your code.

  • Sohail Ahmed Profile Picture
    11,150 Super User 2025 Season 2 on at
    One more suggestion — avoid posting any confidential data (like client IDs, secrets, usernames, or tokens) in the community. Instead, replace them with asterisks (*) or placeholders to keep your information safe from misuse.
  • KK-21020735-0 Profile Picture
    101 on at
    Thank you Sohail
     
    I am not good in JSON handling , I know only  AL ,  If you can give me
    corrections in red color, in the same document. It would be more helpful.
     
    I thank for that. I will practice to code later , your suggestions as reference.
     
    Job finished executing.
    Status: Error
    Error: Misused header name, 'Content-Type'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
     
    Thanks in Advance
     
    Warm regards
    Kris
  • KK-21020735-0 Profile Picture
    101 on at
    Thank you Sohail,'
     
    I will keep in mind and will be careful next time.
     
    Warm Regards
    Kris
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,601 Super User 2025 Season 2 on at
    You're adding 'Content-Type' to the wrong place. It must go on the HttpContent, not the HttpRequestMessage. 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans