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

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

  • robilcirobilci Member Posts: 1
    edited 2019-06-05
    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 response
  • julkifli33julkifli33 Member Posts: 1,073
    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-type
    HttpClient.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
    ihdczktlbon6.png

    tested in postman no issue.
    any idea?

    this is their API
    mk6zf58i1r27.png

  • ftorneroftornero Member Posts: 522
    Hello @julkifli33,

    If you write your code like the solution that @robilci post in his answer then must work.

    Regards
  • julkifli33julkifli33 Member Posts: 1,073
    Hi @ftornero
    i managed to solve this issue.
    Add content type must be added after writeform

    annx963pnj9r.png

    Thanks.
Sign In or Register to comment.