2 Replies Latest reply on Sep 20, 2011 5:56 AM by John Hawkinson

    HTTP POST Request

    Zoffix222 Level 1

      Hey,

       

      I need to do an HTTP POST to a webpage, and store server's output into a variable.

       

      I found this post: http://forums.adobe.com/message/3692371#3692371

       

      However, I have a lot of data to post, and not really in the mood to peruse HTTP specs to find out how to do the POST request via sockets.

       

      Does anyone have an already-written code for this, or better yet, is there a simpler way to do what I want?

        • 1. Re: HTTP POST Request
          Zoffix222 Level 1

          Well, I ended up writing my own

           

          It's not the spotless code and there's no error checking, but it does what I want it to do just fine.

           

          var response = make_HTTP_post(
              'site.com',
              '/page.pl',
              {
                  'ajax': 1,
                  'p': 'lalalala lots of data here',
                  'pass': QPASS,
              }
          );
          
          alert(response);
          
          
          
          function make_HTTP_post(domain, path, post_data) {
          
              var data_string = '';
              for ( var key in post_data ) {
                  if ( post_data.hasOwnProperty(key) ) {
                      data_string += '&' + encodeURIComponent(key)
                                      + '=' + encodeURIComponent(post_data[key]);
                  }
              }
              data_string = data_string.substring(1); // discard first '&'
          
              var conn = new Socket;
              conn.open(domain + ':80');
              conn.write('POST ' + path + " HTTP/1.1\r\n");
              conn.write('Host: ' + domain + "\r\n");
              conn.write('Content-Length: ' + data_string.length + "\r\n");
              conn.write("Content-Type: application/x-www-form-urlencoded\r\n");
              conn.write("\r\n");
          
              conn.write(data_string);
          
          
              var reply = '';
              reply = conn.read(999999);
              conn.close();
          
              reply = reply.split("\n\n");
              reply.splice(0,1); // discard server headers
              return reply.join("\n\n");
          }
          

           

           

          Cheers!

          • 2. Re: HTTP POST Request
            John Hawkinson Level 5

            Good for you. Two brief shooting-from-the-hip comments:

             

             

            It's not the spotless code and there's no error checking, but it does what I want it to do just fine.

            It's almost always better to put the error checking in from the start, you'll save yourself pain later.

             

             

                var reply = '';
                reply = conn.read(999999);
                conn.close();

                reply = reply.split("\n\n");
                reply.splice(0,1); // discard server headers

            It's usually a bad idea to use the same variable to represent two different types. reply starts out as a string, and later on it is an array of strings. That tends to promote confusion, especially as the program gets larger. At any given moment, you can't tell if it is a string or an array of strings without reading around for context. I would recommend using two seperate variables, such as replystr and replyarr, etc.

            1 person found this helpful