HttpWebRequestMgt.AddBodyAsAsciiText is working HttpWebRequestMgt.AddBodyAsText not!
            
                
                    davhol86                
                
                    Member Posts: 6                
            
                        
            
                    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.

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:
                
                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.

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);
0                
            Answers
- 
            I would try to add
; charset=UTF-8
to the content type. If this does not work with SetContentType, tryHttpWebRequestMgt.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.0 - 
            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:
and when i send with BC it looks like that:
                        0 - 
            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.0 - 
            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?0 - 
            Hi,
I solved it now by usingHttpWebRequestMgt.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;
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
 - 323 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
 
