Getting Error in HTTP Post Request in NAV 2016

azharsaeedkhanazharsaeedkhan Member Posts: 37
edited 2024-05-27 in NAV Three Tier
Hello!

I am trying to make an http post request with content type = 'application/x-www-form-urlencoded' but it's giving the Bad Request error.
On postman the same request is working fine.

Please have a look at my code below and suggest what i am doing wrong.


GetAccessToken(var AccessToken): Boolean

CLEAR(ContentTxt);
CLEAR(ResponseTxt);
Application.GET();


ContentTxt := 'grant_type=' + UrlEncode.UrlEncode('password') +
'&client_id=' + UrlEncode.UrlEncode(Application."Client ID") +
'&username=' + UrlEncode.UrlEncode(Application."User Name") +
'&password=' + UrlEncode.UrlEncode(Application.Password) +
'&scope=' + UrlEncode.UrlEncode(Application.Scope) +
'&_tenant=' + UrlEncode.UrlEncode(Application."Tenant Value");


TokenEndPointURL := Application."Access Token URL";
HttpWebRequestMgt.Initialize(TokenEndPointURL);

HttpWebRequestMgt.SetContentType('application/x-www-form-urlencoded');
HttpWebRequestMgt.SetReturnType('application/json');

HttpWebRequestMgt.SetMethod('POST');


HttpWebRequestMgt.AddBodyAsText(ContentTxt);

RequestBodyBlob.INIT;
RequestBodyBlob.Blob.CREATEINSTREAM(InStreamVar);

IF HttpWebRequestMgt.GetResponse(InStreamVar, HttpStatusCode, ResponseHeaders) THEN BEGIN
IF HttpStatusCode.ToString = HttpStatusCode.OK.ToString THEN BEGIN
InStreamVar.READTEXT(ResponseTxt);
IsSuccess := TRUE;
END ELSE BEGIN
InStreamVar.READTEXT(ResponseTxt);
IsSuccess := FALSE;
END;
END ELSE BEGIN
HttpWebRequestMgt.ProcessFaultResponse(TokenEndPointURL);
InStreamVar.READTEXT(ResponseTxt);
MESSAGE(ResponseTxt);
END;

IF IsSuccess THEN BEGIN
CLEAR(AccessToken);
JsonObject := JsonObject.Parse(ResponseTxt);
AccessToken := JsonObject.GetValue('access_token').ToString;
EXIT(IsSuccess);
END ELSE
MESSAGE('%1', ResponseTxt);





I have also wrote another version of the above function, but it's not working as well

GetAccessToken(var AccessToken): Boolean
CLEAR(ContentTxt);
CLEAR(ResponseTxt);
Application.GET(); // Fetch application details

// Construct the POST data
ContentTxt := 'grant_type=' + UrlEncode.UrlEncode('password') +
'&client_id=' + UrlEncode.UrlEncode(Application."Client ID") +
'&username=' + UrlEncode.UrlEncode(Application."User Name") +
'&password=' + UrlEncode.UrlEncode(Application.Password);
'&scope=' + UrlEncode.UrlEncode(Application.Scope) +
'&_tenant=' + UrlEncode.UrlEncode(Application."Tenant Value");

TokenEndPointURL := Application."Access Token URL";

// Create the HttpWebRequest
HttpWebRequest := HttpWebRequest.Create(TokenEndPointURL);
HttpWebRequest.Method := 'POST';
HttpWebRequest.ContentType := 'application/x-www-form-urlencoded';

// Convert ContentTxt to byte array
ContentBytes := Encoding.UTF8.GetBytes(ContentTxt);
HttpWebRequest.ContentLength := Encoding.UTF8.GetBytes(ContentTxt).Length;//ContentBytes.Length;

// Write the POST data to the request stream
Stream := HttpWebRequest.GetRequestStream();
StreamWriter := StreamWriter.StreamWriter(Stream);
StreamWriter.Write(ContentTxt);
StreamWriter.Close();

// Get the response
HttpWebResponse := HttpWebRequest.GetResponse();
Stream := HttpWebResponse.GetResponseStream();
StreamReader := StreamReader.StreamReader(Stream);
ResponseTxt := StreamReader.ReadToEnd();
StreamReader.Close();
HttpWebResponse.Close();

// Display the response
MESSAGE(ResponseTxt);



jaoza6neaoml.png
34ff4nk9hpzv.png

Best Answer

  • azharsaeedkhanazharsaeedkhan Member Posts: 37
    Answer ✓
    ftornero wrote: »
    Hello @azharsaeedkhan,

    I don't see anything wrong in then first version of your code,

    In the second one I would change this
    StreamWriter.Write(ContentTxt);
    

    for that
    StreamWriter.WriteLine(ContentTxt);
    

    And woudn't pass the ContentLength

    What error do you get ?

    Regards

    Thank you for your help. I was actually missing one underscore in _tenant, it was two underscores with tenant (__tenant).

Answers

  • azharsaeedkhanazharsaeedkhan Member Posts: 37
    @ftornero can you please help?
  • ftorneroftornero Member Posts: 524
    Hello @azharsaeedkhan,

    I don't see anything wrong in then first version of your code,

    In the second one I would change this
    StreamWriter.Write(ContentTxt);
    

    for that
    StreamWriter.WriteLine(ContentTxt);
    

    And woudn't pass the ContentLength

    What error do you get ?

    Regards
  • azharsaeedkhanazharsaeedkhan Member Posts: 37
    Answer ✓
    ftornero wrote: »
    Hello @azharsaeedkhan,

    I don't see anything wrong in then first version of your code,

    In the second one I would change this
    StreamWriter.Write(ContentTxt);
    

    for that
    StreamWriter.WriteLine(ContentTxt);
    

    And woudn't pass the ContentLength

    What error do you get ?

    Regards

    Thank you for your help. I was actually missing one underscore in _tenant, it was two underscores with tenant (__tenant).
Sign In or Register to comment.