In Business Central- Cloud- Error on connecting External WS- Http Content

NAVBasic
Member Posts: 8
I am trying to make a connection to external WS and I get an error related to - Misused Header name.
I believe this is due to "Content-Type" and may be the syntax is incorrect.???
I have tried to access the same WS via Postman and I was able to get a success message.…
Any Ideas what is wrong here?
TIA
//Header
header.Clear();
header.Add('Content-type', 'application/json');
header := myclient.DefaultRequestHeaders();
header.Add('username', 'USERNAME');
header.Add('password', 'PASSWORD');
header.Add('UserAgent', 'Apache-HttpClient/4.1.1 (java 1.5)');
header.Add('Connection', 'KeepAlive');
//CONTENT
content.Clear();
content.GetHeaders(header);
content.WriteFrom(_JSonText); // Created a Json Object Request and converted to text.
request.Content := content;
request.Content.GetHeaders(header);
request.GetHeaders(header);
request.SetRequestUri(ReqURL);
request.Method := 'POST';
myclient.Post(ReqURL, content, response);
Message(_JSonText);
response.Content().ReadAs(http_Response);
Message(http_Response);
IF response.HttpStatusCode = 200 then
Message('Success')
ELSE
Message('Error');
I believe this is due to "Content-Type" and may be the syntax is incorrect.???
I have tried to access the same WS via Postman and I was able to get a success message.…
Any Ideas what is wrong here?
TIA
//Header
header.Clear();
header.Add('Content-type', 'application/json');
header := myclient.DefaultRequestHeaders();
header.Add('username', 'USERNAME');
header.Add('password', 'PASSWORD');
header.Add('UserAgent', 'Apache-HttpClient/4.1.1 (java 1.5)');
header.Add('Connection', 'KeepAlive');
//CONTENT
content.Clear();
content.GetHeaders(header);
content.WriteFrom(_JSonText); // Created a Json Object Request and converted to text.
request.Content := content;
request.Content.GetHeaders(header);
request.GetHeaders(header);
request.SetRequestUri(ReqURL);
request.Method := 'POST';
myclient.Post(ReqURL, content, response);
Message(_JSonText);
response.Content().ReadAs(http_Response);
Message(http_Response);
IF response.HttpStatusCode = 200 then
Message('Success')
ELSE
Message('Error');
0
Answers
-
Hello @NAVBasic,
It's better to remove the 'Content-Type' (T in uppercase) before adding the value, like:header.Remove('Content-Type''); header.Add('Content-Type', 'application/json');
By callingmyclient.Post(ReqURL, content, response);
You don't use the request variable at all, you must use:myclient.Send(request, response);
And separate the content header and the request header, 'Content-Type' is part of the content header, and the username, password, etc are part of the request header.
Regards.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