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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

How to POST JSON to an API

(0) ShareShare
ReportReport
Posted on by 555

Hi experts,

This is Business Central cloud.

I'm trying to send a POST HttpRequestMessage to an API.

The body of the message must contain a JSON object, that I have created.

When I run the code, I get this error:

{"type":""title":"Unsupported">tools.ietf.org/.../rfc7231 Media Type","status":415,"traceId":"|f8ca5273-44d86b2bea279d2c."}

On tools.ietf.org/.../rfc7231 I can read this:

"The 415 (Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly."

If I try to send the request to https://ptsv2.com/ as a test, I can see that I am sending the text below. I note that Content-Type has changed from application/json to text/plain. Is that the error?

You can see my code at the bottom.

Hope you can help.

Morten

Details
-------
ID: 4669426974588928
Timestamp: 2022-09-21 11:23:47.715465 +0000 UTC
Method: POST
IP: 127.0.0.1:33960

Headers
-------
X-Cloud-Trace-Context: 245f3c995d1a103ac8d27ce51b1d990c/6622307554941142938
X-Google-Apps-Metadata: domain=gmail.com,host=ptsv2.com
X-Google-Serverless-Node-Envoy-Config-Gae:
Authorization: bearer esdfksjflwej......
Cache-Control: no-cache
Content-Length: 84
Content-Type: text/plain; charset=utf-8
Forwarded: for="13.79.242.159";proto=https
Traceparent: 00-245f3c995d1a103ac8d27ce51b1d990c-5be729a27da7079a-00
X-Forwarded-For: 127.0.0.1,13.79.242.159, 169.254.1.1
X-Forwarded-Proto: https

Parameters
----------
No Parameters

Body
----
{"data":{"Kind":1,"ActorCSID":74,"ProdConceptID":38},"options":{"Labels":"PNG"}}

Files
-----
No files

Multipart Values
----------------
No Multipart Values


procedure SendShipment(Token: Text)
var
CompanyInformation: Record "Company Information";
TypeHelper: Codeunit "Type Helper";
HttpClient: HttpClient;
HttpContent: HttpContent;
HttpHeadersContent, HttpHeadersRequestMessage: HttpHeaders;
HttpRequestMessage: HttpRequestMessage;
HttpResponseMessage: HttpResponseMessage;
Parameters, ResponseText: Text;
JsonObject, DataJsonObject, OptionsJsonObject: JsonObject;
JsonToken: JsonToken;
Content: Text;
begin
CompanyInformation.Get();

HttpRequestMessage.Method := 'POST';
HttpRequestMessage.SetRequestUri('ptsv2.com/.../post');

HttpContent.GetHeaders(HttpHeadersContent);
HttpHeadersContent.Clear();
HttpHeadersContent.Add('Content-Type', 'application/json');

HttpRequestMessage.GetHeaders(HttpHeadersRequestMessage);
HttpHeadersRequestMessage.Add('Cache-Control', 'no-cache');
HttpHeadersRequestMessage.Add('Authorization', 'bearer ' + Token);

DataJsonObject.Add('Kind', 1);
DataJsonObject.Add('ActorCSID', 74);
DataJsonObject.Add('ProdConceptID', 38);

OptionsJsonObject.Add('Labels', 'PNG');

JsonObject.Add('data', DataJsonObject);
JsonObject.Add('options', OptionsJsonObject);

JsonObject.WriteTo(Content);

HttpContent.WriteFrom(Content);

HttpRequestMessage.Content := HttpContent;

HttpClient.Send(HttpRequestMessage, HttpResponseMessage);

I have the same question (0)
  • Suggested answer
    Amit Baru Profile Picture
    3,043 on at

    Hi,

    Pls read the below article

    https://erpconsultors.com/create-json-file-in-al-for-d365-bc/

    Regards

    Amit Sharma

    www.erpconsultors.com

  • Morten Steengaard Profile Picture
    555 on at

    Hi Amit,

    Thank you for your reply.

    The JSON object, I create is fine. But when I send the HTTPRequest to the API, I get the error.

    If I only put "Hello" in HttpContent, then I get the same error. So that must mean that there is no problem with the JSON object.

    Morten

  • Suggested answer
    Vaishnavi J Profile Picture
    3,096 on at

    Hi try the below code

    if this works Also try to use debugger to check if the values and token, content type and data is passing correctly or not.

    procedure SendShipment(Token: Text)

    var

    CompanyInformation: Record "Company Information";

    TypeHelper: Codeunit "Type Helper";

    HttpClient: HttpClient;

    HttpContent: HttpContent;

    HttpHeadersContent, HttpHeadersRequestMessage: HttpHeaders;

    HttpRequestMessage: HttpRequestMessage;

    HttpResponseMessage: HttpResponseMessage;

    Parameters, ResponseText: Text;

    JsonObject, DataJsonObject, OptionsJsonObject: JsonObject;

    JsonToken: JsonToken;

    Content: Text;

    ContentHeader: HTTPHeader;

    begin

    CompanyInformation.Get();

    HttpRequestMessage.Method := 'POST';

    HttpRequestMessage.SetRequestUri('ptsv2.com/.../post');

    HttpContent.GetHeaders(HttpHeadersContent);

    HttpHeadersRequestMessage.Add('Authorization', 'bearer ' + Token);

    HttpContent.getHeaders(ContentHeader);

    ContentHeader.clear();

    ContentHeader.Add('Content-Type', 'application/json');

    DataJsonObject.Add('Kind', 1);

    DataJsonObject.Add('ActorCSID', 74);

    DataJsonObject.Add('ProdConceptID', 38);

    OptionsJsonObject.Add('Labels', 'PNG');

    JsonObject.Add('data', DataJsonObject);

    JsonObject.Add('options', OptionsJsonObject);

    JsonObject.WriteTo(Content);

    HttpContent.WriteFrom(Content);

    HttpRequestMessage.Content := HttpContent;

    HttpClient.Send(HttpRequestMessage, HttpResponseMessage);

    If my answer was helpful to you, please verify it so that other users know it worked. Thank you very much.

  • Morten Steengaard Profile Picture
    555 on at

    Hi Vaishnavi Joshi,

    Thank you very much! You've helped me to find out that when I do this:

    HttpRequestMessage.Content := HttpContent;

    the "Content-Type" is set to "text/plain; charset=utf-8".

    So the solution is to set "Content-Type" after that line. So now my code ends with this:

           HttpContent.WriteFrom(Content);

           HttpContent.GetHeaders(HttpHeadersContent);

           HttpHeadersContent.Clear();

           HttpHeadersContent.Add('Content-Type', 'application/json');

           HttpRequestMessage.Content := HttpContent;

           HttpClient.Send(HttpRequestMessage, HttpResponseMessage);

    Best regards,

    Morten

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,344 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,742 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,231

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans