HttpWebRequestMgt.AddBodyAsAsciiText is working HttpWebRequestMgt.AddBodyAsText not!

Hi All,

I'm using HttpWebRequestMgt with a json body to call a rest api.
It works when I add the body with function "AddBodyAsAsciiText", but i want to send some text in german and that is not working with special chars like ä ü ö.
So i tried to use AddBodyAsText, so the text is utf-8 encoded. But then it's not working anymore.

I'm getting a remote server error (400): invalid request.

1ghdfm9dtdnc.png

With Postman it works fine, also the special chars are send correctly. So it's not a problem of the server, i think.

Has anyone an idea?

here is the code snipped i use:
  //  Create Request
  HttpWebRequestMgt.Initialize(Url);
  HttpWebRequestMgt.DisableUI();
  HttpWebRequestMgt.SetMethod('PATCH');
  HttpWebRequestMgt.SetReturnType('application/json');
  HttpWebRequestMgt.SetContentType('application/json');
  // Here i want to use AddBodyAsText
  HttpWebRequestMgt.AddBodyAsAsciiText(JsonBody);
  
  AddAuthorizationHeader(HttpWebRequestMgt);

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

Answers

  • vaprogvaprog Member Posts: 1,007
    edited 2022-07-06
    I would try to add
    ; charset=UTF-8
    
    to the content type. If this does not work with SetContentType, try
    HttpWebRequestMgt.AddHeader('Content-Type', 'application/json; charset=UTF-8')
    
    instead.

    If this does not help, try to capture the request as it is transferred over the wire, and check your body is valid UTF-8.
  • davhol86davhol86 Member Posts: 4
    Hi @vaprog,

    thank you for you're response. I just tried the first and second approach before, its not working.

    Now I'm trying the third approach. I've found a website which shows me all the header and body data i send and i recognized differences in the format of the body, but I can't get the rigth conclusions from that.

    When i send with Postman than the body looks like that:
    mspfii18n3or.png

    and when i send with BC it looks like that:
    s52ru474sqhn.png
  • vaprogvaprog Member Posts: 1,007
    Try to look at the raw data. As JSON is text, this is not too cryptic.

    From what I see I quess your body is wrapped in a superfluous set of quotes and thus treated as a JSON string, rather than parsed as an object.
  • davhol86davhol86 Member Posts: 4
    I was able to catch the raw data of my request and the body is the same in both requests.
    I was also able to get more info about the bad request error which says "{"errors":[{"code":"0","status":"400","title":"Bad Request","detail":"The JSON payload is malformed."}]}".

    What i recognized is that the Content-Length is different. The request with the error has allways 3 bytes more.

    Here's the payload:
    {
      "name": "TEST Kategorie Äüö",
      "displayNestedProducts": true,
      "productAssignmentType": "product",
      "type": "page",
      "active": true,
      "visible": true,
      "parentId": "9aaff3ff49534e84b59b83fc04afd1c3"
    }
    

    Has someone an idea what i can try next?
  • davhol86davhol86 Member Posts: 4
    Hi,

    I solved it now by using
    HttpWebRequestMgt.AddBodyBlob(TempBlob);
    
    insteat.

    Below are the 2 Functions. AddBodyAsTextWithEncoding uses a StreamWriter to write the body while AddBodyBlob copies the stream from blob.
    I assume it's cause a StreamWriter writes body as text and the COPYSTREAM function writes the body as a bytestream.

    Now the Content-Length is also the same as with Postman.
    AddBodyBlob(VAR TempBlob : Record TempBlob)
    IF NOT TempBlob.Blob.HASVALUE THEN
      EXIT;
    
    RequestStr := HttpWebRequest.GetRequestStream;
    TempBlob.Blob.CREATEINSTREAM(BlobStr);
    COPYSTREAM(RequestStr,BlobStr);
    RequestStr.Flush;
    RequestStr.Close;
    RequestStr.Dispose;
    
    LOCAL AddBodyAsTextWithEncoding(BodyText : Text;Encoding : DotNet "System.Text.Encoding")
    RequestStr := HttpWebRequest.GetRequestStream;
    StreamWriter := StreamWriter.StreamWriter(RequestStr,Encoding);
    StreamWriter.Write(BodyText);
    StreamWriter.Flush;
    StreamWriter.Close;
    StreamWriter.Dispose;
    
Sign In or Register to comment.