Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

Getting Http status 415 Unsupported Media type error with JSON POST error on the API Call

Posted on by
 
Hi All,
                I'm trying to make the Http POST on the business central with below Code.  
procedure pushworkorder(): text
    var
        TypeHelper: Codeunit /Type Helper/;
        JSONManagement: Codeunit /JSON Management/;
        ContentHeaders: HttpHeaders;
        Client: HttpClient;
        //RequestContent: HttpContent;
        Content: HttpContent;
        ResponseMessage: HttpResponseMessage;
        RequestMessage: HttpRequestMessage;
        ToContactNo: Text[30];
        Messagetxt: Text[100];
        URL: Text[200];
        AccessToken: Text;
        JsonText: Text;
        RequestContentTxt: JsonObject;
        JsonData: Text;
        Text001: Label 'Message Sent Successfully';
        Text002: Label 'Error: ';
    begin
        URL := '{URL}';
        AccessToken := '{AccessToken}';
        RequestContentTxt.Add('cr63a_jobnocbs', 'YR-J-23-00004');
        RequestContentTxt.Add('cr63a_jobdescriptionscbs', 'Job Description Code CBS Through API');
        RequestContentTxt.Add('cr63a_jobtasklinenocbs', '20001');
        RequestContentTxt.Add('cr63a_jobtasknocbs', '1010');
        RequestContentTxt.Add('msdyn_serviceaccount@odata.bind', '/accounts(028b307b-c526-ee11-9965-6045bd6972c7)');
        RequestContentTxt.Add('msdyn_systemstatus', 690970000);
        RequestContentTxt.Add('msdyn_workordertype@odata.bind', '/msdyn_workordertypes(e8266b7a-fafa-ed11-8f6e-6045bd6972c7)');
        RequestContentTxt.Add('msdyn_workordersummary', 'Work Order Summary Code CBS Through API');
        RequestContentTxt.WriteTo(JsonData);
        Content.GetHeaders(ContentHeaders);
        ContentHeaders.Clear();
        ContentHeaders.Remove('Content-Type');
        ContentHeaders.Add('Content-Type', 'application/json');
        ContentHeaders.Add('OData-Version', '4.0');
        ContentHeaders.Add('OData-MaxVersion', '4.0');
        Client.DefaultRequestHeaders.Add('Accept', 'application/json');
        Client.DefaultRequestHeaders.Add('Authorization', StrSubstNo('Bearer %1', AccessToken));
        Content.WriteFrom(JsonData);
        RequestMessage.Content := Content;
        RequestMessage.SetRequestUri(URL);
        RequestMessage.Method := 'POST';
        Client.Send(RequestMessage, ResponseMessage);
        ResponseMessage.Content.ReadAs(JsonText);
        if not ResponseMessage.IsSuccessStatusCode() then
            Error('Failed to Post. Reason => ' + ResponseMessage.ReasonPhrase);
        exit(JsonText);
    end;
While Debugging, I'm getting the below Response only. 
HttpStatusCode:415
 
 
 
ReasonPhrase:Unsupported Media Type
 
 
 
IsSuccessStatusCode:False
 
 
 
IsBlockedByEnvironment:False
 
 
 
Headers
 
 
 
 
Cache-Control:no-cache
 
 
 
 
x-ms-service-request-id:e17c9f77-b28d-4035-9aa1-f242ba535eb3;98e76771-00a4-4149-8bdc-86cea1d7469d
 
 
 
 
Set-Cookie:ARRAffinity=19bdbafc0d65e4ef3f5bdb22f7ea91b0e2a767db4bd5f7070b65ac96605751f65ab954b42cfa12ad9f33ddc80a07e15dd633bd57139a65479392e46e5242529408DB8E928EE03FA81098300741; domain=yallafssb01.crm15.dynamics.com; path=/; secure; HttpOnly
 
 
 
 
Strict-Transport-Security:max-age=31536000; includeSubDomains
 
 
 
 
REQ_ID:98e76771-00a4-4149-8bdc-86cea1d7469d
 
 
 
 
AuthActivityId:e40523c2-5f1e-4c1f-a8bd-bf59f5a8c07c
 
 
 
 
x-ms-dop-hint:48
 
 
 
 
x-ms-ratelimit-time-remaining-xrm-requests:1,200.00
 
 
 
 
x-ms-ratelimit-burst-remaining-xrm-requests:7999
 
 
 
 
OData-Version:4.0
 
 
 
 
X-Source:2292336419021485198160103137477915945255111161997321815722724613513188299215664161159;1651112724517753191487153766223873140752202533417150161172341772511014021069103101
 
 
 
 
Public:OPTIONS,GET,HEAD,POST
 
 
 
 
Date:Thu, 27 Jul 2023 10:43:51 GMT
 
Please Anyone Help me to solve this issue. You help is highly Appreciated.
 
Categories:

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,219 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans