Content-Length calculation wrong when posting UTF-8 content
The current project is driving me crazy ...
I carefully followed the "Unicode Best-Practice" guide (https://support.smartbear.com/readyapi/docs/testing/best-practices/unicode-support.html) which cured my problem when reading(!) some(!) UTF-8 encoded characters within a RESPONSE.
The
-Dfile.encoding=UTF8
in the *.vmoptions did the trick. Would make a good default setting...
I even got the JSON POST reply formatted as UTF-8 correctly by specifically specifying the UTF-8 encoding in the REST properties.
But I can't get the "Content-Length" header correct. It always is a few counts short. Either it's because the data indeed contains UTF-8 multy-byte characters or it is counting line-breaks wrong. It might count "characters" instead of "bytes". My back-end complains with a 500 error.
Shouldn't the above setting care for the Content-Length too?
If I watch the communication in Fiddler, copy over the request content to notepad++ to see the actual length, manually change the Content-Length and re-issue the post, my back-end finally does accept the POST.
So what might I still missing?
As it turned out, SOAPUI DID calculate the Content-Length correct. However the back end service did not.
The confusion was about the line endings. The header lines generated by SOAPUI were CRLF, the body however was LF only. Both the server and notepad++ did expand them to CRLF and obviously the server did count the expanded content, missing a byte for every line in the body.
I did a replacement on the body content before I sent the request://replace all wrong line-endings: \n --> \r\n body = body.replace("\r\n","\n").replace("\n","\r\n") def request = testRunner.testCase.getTestStepByName("UpdateVariables") as RestTestRequestStep //set body request.testRequest.setRequestContent(body)
Afterwards the content was a few bytes larger and the server was happy.
Maybe there is a more simple solution to have all line endings the same - and in a defined manner.