How to POST a JSON to an API

MortenSteengaard
Member Posts: 144
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);
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);
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions