Unable to send Post request in Business Central

stuart62stuart62 Member Posts: 2
This is my AL code:

var
Client: HttpClient;
Headers: HttpHeaders;
Content: HttpContent;
Url: Text;
MyContent: Text;
TempBlob: Record TempBlob;
APIResult: Boolean;APIOutput: Text;
AuthText: Text;
begin
Client.DefaultRequestHeaders.Add('Accept', 'application/json');
Content.Clear();
Content.WriteFrom(MyContent);
Headers.Clear();
AuthText := StrSubstNo('username:password');
TempBlob.WriteAsText(AuthText, TextEncoding::Windows);
Headers.Add('Authorization', StrSubstNo('Basic %1', TempBlob.ToBase64String()));
Headers.Add('Content-Type', 'application/json');
Content.GetHeaders(Headers);
if not Client.Post(Url, Content, ResponseMessage) then begin
Error('The call to the web service failed.');
end;
Headers := ResponseMessage.Headers();
ResponseMessage.Content().ReadAs(APIOutput);
Message(APIOutput);
EXIT(ResponseMessage.IsSuccessStatusCode());

At the Headers.Add('Content-Type', 'application/json'); get the following error:
Element 'Headers' is not a member of 'Content'

Think this is caused by the previous line, been unable to find anywhere online where this error has occurred previously. Been struggling with this for days, have tried without the authorization but still get the same error.

Answers

  • stuart62stuart62 Member Posts: 2
    Resolved was all to do do with allowing http requests not enabled and throwing unexpected errors.
Sign In or Register to comment.