Getting Error in HTTP Post Request in NAV 2016

azharsaeedkhan
Member Posts: 37
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);


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


0
Best Answer
-
Hello @azharsaeedkhan,
I don't see anything wrong in then first version of your code,
In the second one I would change thisStreamWriter.Write(ContentTxt);
for thatStreamWriter.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).
0
Answers
-
@ftornero can you please help?0
-
Hello @azharsaeedkhan,
I don't see anything wrong in then first version of your code,
In the second one I would change thisStreamWriter.Write(ContentTxt);
for thatStreamWriter.WriteLine(ContentTxt);
And woudn't pass the ContentLength
What error do you get ?
Regards0 -
Hello @azharsaeedkhan,
I don't see anything wrong in then first version of your code,
In the second one I would change thisStreamWriter.Write(ContentTxt);
for thatStreamWriter.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).
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions