Loading ...
Sorry, an error occurred while loading the content.

Escape Characters added in C# POST of JSON string to PHP Server

Expand Messages
  • dickwmsinnc
    I m using the following C# code to do a POST to a php server. DataContractJsonSerializer ser = new DataContractJsonSerializer(rbnData.GetType()); MemoryStream
    Message 1 of 4 , Oct 16, 2011
    • 0 Attachment
      I'm using the following C# code to do a POST to a php server.

      DataContractJsonSerializer ser = new DataContractJsonSerializer(rbnData.GetType());
      MemoryStream ms = new MemoryStream();
      ser.WriteObject(ms, rbnData);
      String json = Encoding.UTF8.GetString(ms.ToArray());

      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.xxxx.net/rx2.php");
      req.Method = "POST";
      req.ContentType = "application/x-www-form-urlencoded";
      string postData = "spt=" + json;
      req.ContentLength = postData.Length;

      StreamWriter stOut = new
      StreamWriter(req.GetRequestStream(),
      System.Text.Encoding.ASCII);
      stOut.Write(postData);
      stOut.Close();

      My json string is: {"e":"W3OA","h":"bb943d46","s":[["24898.1","K2TQC","1","19","24","False"]],"t":"s"}

      The server shows the following result: [spt] => {\\"e\\":\\"W3OA\\",\\"h\\":\\"bb943d46\\",\\"s\\":[[\\"24898.1\\",\\"K2TQC\\",\\"1\\",\\"19\\",\\"24\\",\\"False\\"]],\\"t\\":\\"s\\"}

      If I don't precede the json string with "spt=" the server gets an empty string.

      I'd like to know where those extra "\" characters are inserted and how I can avoid this.

      Thank you - Dick
    • Stephan Beal
      ... The server shows the following result: [spt] = ... How are you reading this variable server-side? If you post form-urlencoded data (as you are above) then
      Message 2 of 4 , Oct 16, 2011
      • 0 Attachment
        On Sun, Oct 16, 2011 at 7:15 PM, dickwmsinnc <w3oa@...> wrote:

        > **
        >
        > My json string is:
        > {"e":"W3OA","h":"bb943d46","s":[["24898.1","K2TQC","1","19","24","False"]],"t":"s"}
        >
        The server shows the following result: [spt] =>
        > {\\"e\\":\\"W3OA\\",\\"h\\":\\"bb943d46\\",\\"s\\":[[\\"24898.1\\",\\"K2TQC\\",\\"1\\",\\"19\\",\\"24\\",\\"False\\"]],\\"t\\":\\"s\\"}
        >
        > If I don't precede the json string with "spt=" the server gets an empty
        > string.
        >

        How are you reading this variable server-side? If you post form-urlencoded
        data (as you are above) then $_POST should work. However, you are simply
        using:

        string postData = "spt=" + json;

        without actually encoding json as form-urlencoded (not that i can see from
        your code, anyway), so i'm curious as to any of it comes out right.

        If you want to post raw, unencoded JSON then:

        a) Set the request content-type of application/json
        b) POST it as a plain/unencoded string without the leading spt= part.
        c) on the server you can do:

        $jsonString = @file_get_contents("php://input");
        $jsonObj = json_decode($jsonString);

        That way is much more efficient than form-urlencoding JSON, which is an
        unnecessary step for both the client and server when dealing solely with
        JSON data.

        --
        ----- stephan beal
        http://wanderinghorse.net/home/stephan/


        [Non-text portions of this message have been removed]
      • dickwmsinnc
        Thank you for your quick response. The server operator is away for a few days. We will try your suggestion when he gets back and report the results here.
        Message 3 of 4 , Oct 16, 2011
        • 0 Attachment
          Thank you for your quick response. The server operator is away for a few days. We will try your suggestion when he gets back and report the results here.

          Dick

          --- In json@yahoogroups.com, Stephan Beal <sgbeal@...> wrote:
          >
          > On Sun, Oct 16, 2011 at 7:15 PM, dickwmsinnc <w3oa@...> wrote:
          >
          > > **
          > >
          > > My json string is:
          > > {"e":"W3OA","h":"bb943d46","s":[["24898.1","K2TQC","1","19","24","False"]],"t":"s"}
          > >
          > The server shows the following result: [spt] =>
          > > {\\"e\\":\\"W3OA\\",\\"h\\":\\"bb943d46\\",\\"s\\":[[\\"24898.1\\",\\"K2TQC\\",\\"1\\",\\"19\\",\\"24\\",\\"False\\"]],\\"t\\":\\"s\\"}
          > >
          > > If I don't precede the json string with "spt=" the server gets an empty
          > > string.
          > >
          >
          > How are you reading this variable server-side? If you post form-urlencoded
          > data (as you are above) then $_POST should work. However, you are simply
          > using:
          >
          > string postData = "spt=" + json;
          >
          > without actually encoding json as form-urlencoded (not that i can see from
          > your code, anyway), so i'm curious as to any of it comes out right.
          >
          > If you want to post raw, unencoded JSON then:
          >
          > a) Set the request content-type of application/json
          > b) POST it as a plain/unencoded string without the leading spt= part.
          > c) on the server you can do:
          >
          > $jsonString = @file_get_contents("php://input");
          > $jsonObj = json_decode($jsonString);
          >
          > That way is much more efficient than form-urlencoding JSON, which is an
          > unnecessary step for both the client and server when dealing solely with
          > JSON data.
          >
          > --
          > ----- stephan beal
          > http://wanderinghorse.net/home/stephan/
          >
          >
          > [Non-text portions of this message have been removed]
          >
        • dickwmsinnc
          Your suggestions are just what we needed to get going. We implemented them and are now sending and receiving JSON strings just fine. Thank you for your help.
          Message 4 of 4 , Oct 19, 2011
          • 0 Attachment
            Your suggestions are just what we needed to get going. We implemented them and are now sending and receiving JSON strings just fine.

            Thank you for your help.

            Dick

            --- In json@yahoogroups.com, Stephan Beal <sgbeal@...> wrote:
            >
            > On Sun, Oct 16, 2011 at 7:15 PM, dickwmsinnc <w3oa@...> wrote:
            >
            > > **
            > >
            > > My json string is:
            > > {"e":"W3OA","h":"bb943d46","s":[["24898.1","K2TQC","1","19","24","False"]],"t":"s"}
            > >
            > The server shows the following result: [spt] =>
            > > {\\"e\\":\\"W3OA\\",\\"h\\":\\"bb943d46\\",\\"s\\":[[\\"24898.1\\",\\"K2TQC\\",\\"1\\",\\"19\\",\\"24\\",\\"False\\"]],\\"t\\":\\"s\\"}
            > >
            > > If I don't precede the json string with "spt=" the server gets an empty
            > > string.
            > >
            >
            > How are you reading this variable server-side? If you post form-urlencoded
            > data (as you are above) then $_POST should work. However, you are simply
            > using:
            >
            > string postData = "spt=" + json;
            >
            > without actually encoding json as form-urlencoded (not that i can see from
            > your code, anyway), so i'm curious as to any of it comes out right.
            >
            > If you want to post raw, unencoded JSON then:
            >
            > a) Set the request content-type of application/json
            > b) POST it as a plain/unencoded string without the leading spt= part.
            > c) on the server you can do:
            >
            > $jsonString = @file_get_contents("php://input");
            > $jsonObj = json_decode($jsonString);
            >
            > That way is much more efficient than form-urlencoding JSON, which is an
            > unnecessary step for both the client and server when dealing solely with
            > JSON data.
            >
            > --
            > ----- stephan beal
            > http://wanderinghorse.net/home/stephan/
            >
            >
            > [Non-text portions of this message have been removed]
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.