Rest Call to Cognitive Service Computer vision in AL, Header Issue

vfoeke@hotmail.com
Member Posts: 2
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
}
}
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
}
}
0
Answers
-
Hi, this code work for me, you have to set content header and set "User-agent" propertie:
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 response0 -
Hi @ftornero ,
i am trying to integrate BC Cloud with another 3rd party (https://api.mambu.com).
I get the same error message for content-typeHttpClient.DefaultRequestHeaders.add('Accept', 'application/vnd.mambu.v2+json'); HttpClient.DefaultRequestHeaders.add('apiKey', GetMambuAPIKey()); HttpClient.DefaultRequestHeaders.Add('Content-Type', 'application/json'); HttpClient.DefaultRequestHeaders.add('User-Agent', 'Microsoft-Dynamics-Business-Central'); HttpClient.Send(Request, ResponseMessage);
i got this error message
tested in postman no issue.
any idea?
this is their API
0 -
Hello @julkifli33,
If you write your code like the solution that @robilci post in his answer then must work.
Regards0 -
1
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