How to POST a JSON to an API

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":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"|f8ca5273-44d86b2bea279d2c."}

On https://tools.ietf.org/html/rfc7231#section-6.5.13 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('https://ptsv2.com/t/.../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);
Sign In or Register to comment.