How to change the char-set of POST body

3 posts, 0 answers
  1. Taka
    Taka avatar
    4 posts
    Member since:
    May 2015

    Posted 07 May 2015 Link to this post

    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. Eric Lawrence
    Admin
    Eric Lawrence avatar
    833 posts

    Posted 08 May 2015 Link to this post

    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.

     
  3. Taka
    Taka avatar
    4 posts
    Member since:
    May 2015

    Posted 17 May 2015 in reply to Eric Lawrence Link to this post

    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,
Back to Top