This is a migrated thread and some comments may be shown as answers.

How to change the char-set of POST body

2 Answers 704 Views
Fiddler Classic
This is a migrated thread and some comments may be shown as answers.
Taka
Top achievements
Rank 1
Taka asked on 07 May 2015, 03:00 PM

I am a Japanese user. I want to convert the char-set of request for a web application which supports only non-English char-set because UTF-8 is set as the char-set of POST body from Fiddler Script.

I wrote the following code but I was not able to change the char-set from UTF-8:

static function OnBeforeRequest(oSession: Session) {
    if (oSession.HTTPMethodIs("POST")) {
        var cliEnc = System.Text.Encoding.GetEncoding("utf-8");
        var srvEnc = System.Text.Encoding.GetEncoding("shift_jis"); // "shift_jis" is still often used in Japan...
        oSession.requestBodyBytes = System.Text.Encoding.Convert(cliEnc, srvEnc, oSession.requestBodyBytes);
        oSession.oRequest["Content-Length"] = oSession.requestBodyBytes.Length;
    }
}

Is there the method to change the char-set of POST body?

2 Answers, 1 is accepted

Sort by
0
Eric Lawrence
Telerik team
answered on 08 May 2015, 04:02 PM
Hi, Taka--

This is a complicated topic. You can do something like this:

  if (oSession.HTTPMethodIs("POST") &&
   oSession.oRequest.headers.Exists("Content-Type") &&
   !oSession.oRequest.headers.ExistsAndContains("Content-Type", "charset")) 
  {
var srvEnc = System.Text.Encoding.GetEncoding("shift_jis"); 
   var sOriginal = oSession.GetRequestBodyAsString(); 
     oSession.oRequest["Content-Type"] = (oSession.oRequest["Content-Type"] +"; charset=shift_jis");
    oSession.RequestBody = srvEnc.GetBytes(sOriginal);
  }

This will work just fine for certain types of POST bodies, but it will fail spectacularly if there is non-textual content within the body (for instance, an image file upload).

Generally speaking, web applications should respect the charset attribute of the request body and should default to something reasonable like UTF-8 if the charset is not specified.

Regards,
Eric Lawrence
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Taka
Top achievements
Rank 1
answered on 18 May 2015, 02:09 AM
Hello Eric,

Thanks a lot for your code and I am sorry for my late reply and appreciation.

I was not able to obtain the desired result with your code, but your comments provided me with hint of solution.

The GetRequestBodyAsString() and the requestBodyBytes return/have an already url-encoded string and those codes and the sting consists of only alphanumeric characters. It does not make sense to change char-set of such string because the codes of those characters are common to UTF-8 and Shift-JIS etc.

So after getting an un-encoded string from UnescapeDataString(), I was able to obtain the desired result by url-encoding the string with Shift-JIS.
However, probably I also have to write another script for multipart/form-data.

Best regards,
Tags
Fiddler Classic
Asked by
Taka
Top achievements
Rank 1
Answers by
Eric Lawrence
Telerik team
Taka
Top achievements
Rank 1
Share this question
or