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:
// 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
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.
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:
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.
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:
Has someone an idea what i can try next?
I solved it now by using 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.