Options

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

NAVBasicNAVBasic 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');

Answers

  • Options
    ftorneroftornero Member Posts: 522
    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 calling
    myclient.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.
Sign In or Register to comment.