HttpWebRequestMgt. - Bad Request (400)

Hi There,

I’ve a problem with a Webservice Call. I want to update an entity so I use the ‘PUT’ Method and the HttpWebRequestMgt Codeunit (1297) to create and send the request.
It’s working fine in Postman, but in NAV (2018) I get an HttpErrorCode 400.

I’ve also created a GET Request with the same url and it’s working fine. So I’ve basically only added a JSON-Body and changed the HTTP-Method.

So i assume that i forgot something…here’s the code i’m using:

//  Check/Format Url
IF LOWERCASE(COPYSTR(Account."Transmission URL", 1, 4)) <> 'http' THEN
  Url := STRSUBSTNO('%1%2/api/%3/%4?useNumberAsId=true', 'https://', Account."Transmission URL", LOWERCASE(EntityName), LOWERCASE(Number))
  ELSE
    Url := STRSUBSTNO('%1/api/%2/%3?useNumberAsId=true', Account."Transmission URL", LOWERCASE(EntityName), LOWERCASE(Number));

  //  Create Request
  HttpWebRequestMgt.Initialize(Url);
  HttpWebRequestMgt.DisableUI();
  HttpWebRequestMgt.SetMethod(HttpMethod);
  //HttpWebRequestMgt.SetReturnType('application/json');
  HttpWebRequestMgt.SetContentType('application/json');
  HttpWebRequestMgt.AddBasicAuthentication(Account.User, Account.Password);
  
  // Add JSON Body
  OrderStatusId := 1;
  JsonTextWriter.WriteStartObject('');
  JsonTextWriter.WriteRawProperty('orderStatusId', OrderStatusId);
  JsonTextWriter.WriteEndObject();
  //MESSAGE(JsonTextWriter.GetJSonAsText());
  HttpWebRequestMgt.AddBodyAsText(JsonTextWriter.GetJSonAsText());

HttpWebRequestMgt.SendRequestAndReadTextResponse(ResponseBody, ErrorMsg, ErrorDetails, HttpStatusCode, ResponseHeaders);

Not a solution but more a suggestion -
I haven’t used NAV 2018 so I can’t test your code.

If you have a request that works and one that doesn’t, capture the raw request from the postman console and compare it to the request you’ve made above. Use a test service like requestbin or a variant of it to capture your requests generated in code and compare.
You just want something to log your HTTP requests. Just make sure to be careful on what you send for auth if you are not running the logging locally. A simple google search is your friend for a tool.

Can you use this? https://learning.postman.com/docs/developer/echo-api/

Other ideas:
verify HttpMethod in your code.
Write the URI out and make sure it matches postman
check for case sensitivity in the URL params. (I’ve seen worse…)
V 19.0 has SetJSONContent on HttpWebRequest - not sure if it’s in your version.

1 Like

Sorry, for the late response, but i was working on other topics the last weeks.
Today finally I found some time to spend again with this problem and it paid off. I finally found the solution for my problem. I looked at some examples on the net and instead of using:

  //HttpWebRequestMgt.AddBodyAsText(JsonTextWriter.GetJSonAsText()); 
  HttpWebRequestMgt.AddBodyAsAsciiText(JsonTextWriter.GetJSonAsText());

The difference between these two functions is that in the first row the body is send UTF-8 encoded and in the second it’s ASCII encoded.
Here the code from CU 1297 “Http Web Request Mgt.”:

AddBodyAsText(BodyText : Text)
// Assume UTF8
AddBodyAsTextWithEncoding(BodyText,Encoding.UTF8);

AddBodyAsAsciiText(BodyText : Text)
AddBodyAsTextWithEncoding(BodyText,Encoding.ASCII);

Thank you anyway @JayM for you’re suggestion, i will keep it in mind for the next time.

1 Like

Thank you for this, it helped me solve the same problem.

Thank you for this, I had exactly the same problem!