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 Posts: 1Member
    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
Sign In or Register to comment.