Hi
I'm trying to do a POST Rest call to the computer vision service of the Azure Cognitive services in the new development environment in AL using the HTTPClient as seen in the example of this blog post
http://www.kauffmann.nl/2017/06/24/al-support-for-rest-web-services/.
When using the code underneath I'm having the issue that I cannot pass my "Content Type" header as it gives me the following error. "Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."
When I do not specify this header the Webservice returns "Unsupported media Type" so I'm guessing this header is required.
Do you have any idea how I am able to pass this header in my REST Call to the webservice?
actions
{
addafter("Bank Accounts"){
action(Test)
{
trigger OnAction();
var
HttpClient:HttpClient;
content:HttpContent;
ResponseMessage:HttpResponseMessage;
headers:HttpHeaders;
begin
HttpClient.DefaultRequestHeaders.Add('Ocp-Apim-Subscription-Key','****************************');
// HttpClient.DefaultRequestHeaders.Add('Content-Type','application/json');
content.WriteFrom('{"url": ' + '"' + '
https://www.goodmorningquote.com/wp-content/uploads/2015/03/best-good-morning-quotes-and-images.jpg' + '"}');
//content.GetHeaders(request);
HttpClient.Post('
https://westeurope.api.cognitive.microsoft.com/vision/v1.0/ocr?language=unk&detectOrientation =true',content,ResponseMessage);
Message(ResponseMessage.ReasonPhrase());
end;
}// Add changes to page actions here
}
}
Answers
jsonRequest := '{"propertie1": "value1", "propertie2": "value2"}'; // Your request body ...
content.WriteFrom(jsonRequest);
content.GetHeaders(contentHeaders); // type of object : HttpHeaders
contentHeaders.Remove('Content-Type');
contentHeaders.Add('Content-Type', 'text/json;charset=utf-8'); // It depends of your request
//body type
client.DefaultRequestHeaders.Add('User-Agent', 'Dynamics 365'); // necessary dont forget it !
client.DefaultRequestHeaders.TryAddWithoutValidation('Content-Type', 'text/json'); // response
//type
client.DefaultRequestHeaders.TryAddWithoutValidation('Authorization', 'Basic ' + finalKey);
// Your auth system if you have one
if not client.Post('your url', content, response) // send request here
then
Error('The call to the web service failed.');
if not response.IsSuccessStatusCode then
error('The web service returned an error message:\\' +
'Status code: %1\' +
'Description: %2',
response.HttpStatusCode,
response.ReasonPhrase);
response.Content.ReadAs(JsonText);
Message('Json response success : %1', JsonText); // display your response
i am trying to integrate BC Cloud with another 3rd party (https://api.mambu.com).
I get the same error message for content-type
i got this error message
tested in postman no issue.
any idea?
this is their API
If you write your code like the solution that @robilci post in his answer then must work.
Regards
i managed to solve this issue.
Add content type must be added after writeform
Thanks.