Loading...
Logo
Processing Forum

HTTP post Processing

in Programming Questions  •  3 years ago  
Is there a way to do a http post within processing? Basically I'm trying to replicate a simple php form that inputs a couple fields to a mysql database.

Replies(16)

One simple solution is you can pass all of your input in a url to your php script then the Php script does all the DB work.

I too have the same question. Is it true that there is no way (even using the processing.net.* library) to POST or GET to a remote server? HTTP POST GET and PUT are fundamental to server interaction so there must be a simple way to do this in Processing. If not, what am I missing? Why is this so difficult and what is processing.net.* for of not sending HTTP commands? I must say, I have looked for code samples showing HTTP PUT in Processing and they don't seem to exist...

 Thanks   Al (Santa Clara)
stopfocus, passing data through the URL isn't a POST but a GET operation.
Maybe it is enough for the needs of slushe.
Otherwise, in the save to web --- image export thread, I show the DataUpload.java class which does a POST request. It is to upload data, but if you know the syntax of POST, you can use it for other tasks.
Thanks!
 I am somewhat new to Processing and don't know Java. So... Do I need to load Java libraries into Processing to do an HTML POST? I do appreciate the CRUD aspects of HTML re RESTful ideals.

 I need a little context to the proposed solution. Is there a way to  POST PUT to a remote server using Processing without some form of Java inclusion?   I have used the processing.net.* library to GET and it works but POST and PUT are illusive. Here is a GET code segment (below) so I would expect (perhaps foolishly) for a POST and PUT equivalent. 

Include processing.net.*

c = new Client(this, "http://www.theavitbook.com", 80); // Connect to server on port 80
  c.write("GET / HTTP/1.1\r\n"); // Use the HTTP "GET" command to ask for a Web page
  c.write("Host: www.myserver.com\n\n"); //   who I am

   Al
Update -- correction -- previous post should have used HTTP not HTML in the comments -- missed this when typing.

 Al
You can edit your posts!
And you can mark your question as answered (and choose a best answer).
I just assumed, maybe incorrectly that he was trying to send simple text data. Unless you are sending data or you are going to exceed the size limit of get there is no reason to use post.

Since it seems you are just trying to send data for a sql update just format the url and save your self the trouble.

Otherwise the save image library is cool. I have used it myself.
Unless you are sending data or you are going to exceed the size limit of get there is no reason to use post.
Well, not really. POST and GET have different usages (look for REST protocol), although the difference is more important in an HTML page (with regard to refresh page capability, for example) than in Processing.
But yet, if you need to send data to a server-side script on which you have no control, and if this script expects only a POST request, you have to call it this way.
Since Processing is parsed into Java, you can just use this:

I threw together a working example here:


Copy code
  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.client.methods.HttpPost;
  4. import org.apache.http.impl.client.DefaultHttpClient;

  5. void setup()
  6. {
  7.   String url = "http://hroch486.icpf.cas.cz/cgi-bin/echo.pl";
  8.    
  9.   try
  10.   {
  11.     DefaultHttpClient httpClient = new DefaultHttpClient();

  12.     HttpPost          httpPost   = new HttpPost( url );
  13.     HttpParams        postParams = new BasicHttpParams();
  14.                       postParams.setParameter( "your_name", "John Smith" );
  15.                       postParams.setParameter( "fruit", "Apricot" ); // Configure the form parameters
  16.                       httpPost.setParams( postParams );    
  17.     
  18.     println( "executing request: " + httpPost.getRequestLine() );
  19.     
  20.     HttpResponse response = httpClient.execute( httpPost );
  21.     HttpEntity   entity   = response.getEntity();
  22.     
  23.     
  24.     println("----------------------------------------");
  25.     println( response.getStatusLine() );
  26.     println("----------------------------------------");
  27.     
  28.     if( entity != null ) entity.writeTo( System.out );
  29.     if( entity != null ) entity.consumeContent();

  30.     
  31.     // When HttpClient instance is no longer needed, 
  32.     // shut down the connection manager to ensure
  33.     // immediate deallocation of all system resources
  34.     httpClient.getConnectionManager().shutdown();       
  35.     
  36.   } catch( Exception e ) { e.printStackTrace(); }
  37.    
  38.   exit();
  39. }


Hello,

This would be an extremely helpful code snippet, but I think it does not work. That is, even if I run the sample app, the response contains no parsed values. That is, the URL you are posting this to does not get the POSTed vals.

I have also implemented this via a php script on my website and found that indeed the script does not see anything in $_POST.

Could you please clarify/help with this, or point in the right direction. Thank you!

Sergiy
Thanks Jack and "stopfocus",
 I will try your suggested example. I am familiar with Processing not Java but I will experiment with your code. I appreciate the efforts!

  Al
Thanks jack! That's perfect. 
Processing is based on Java: basically when you write a Processing sketch, you are writing a Java program with just some minor syntactic adjustments.
You can drop a .java file in a sketch, it will become a tab in the PDE, like the other .pde files.
I have solved the problem I had before, and the issue was in HttpParams declaration. Somehow, this did not work. A version that worked for me:
Copy code
  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.client.methods.HttpPost;
  4. import org.apache.http.impl.client.DefaultHttpClient;

  5. void setup()
  6. {
  7.   String url = "http://YOURDOMAIN.com/YOURSCRIPT.php";

  8.   try
  9.   {
  10.     DefaultHttpClient httpClient = new DefaultHttpClient();

  11.     HttpPost          httpPost   = new HttpPost( url );
  12. List nameValuePairs = new ArrayList(2);  
  13.     nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
  14. nameValuePairs.add(new BasicNameValuePair("param2", "value2"));

  15. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

  16.     // println( "executing request: " + httpPost.getRequestLine() );
  17.     HttpResponse response = httpClient.execute( httpPost );
  18.     HttpEntity   entity   = response.getEntity();


  19.     println("----------------------------------------");
  20.     println( response.getStatusLine() );
  21.     println("----------------------------------------");

  22.     if( entity != null ) entity.writeTo( System.out );
  23.     if( entity != null ) entity.consumeContent();


  24.     // When HttpClient instance is no longer needed, 
  25.     // shut down the connection manager to ensure
  26.     // immediate deallocation of all system resources
  27.     httpClient.getConnectionManager().shutdown();       

  28.   } catch( Exception e ) { e.printStackTrace(); }


  29.   exit();
  30. }
Hey,

the script works fine but I have a little problem with the server response. Could anybody say how I can drop the result from console to the compiled sketch as text or manipulate the result? I dont now which column in the code is responsible for the result. In my example is this json object the result: "{"text": "processing"} until then perfect but I'm afraid that i can't edit the content of the json object or use it for an visualization...

THX phi.lho, i will try this variant
Copy code
  1. import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    //import org.json.*;

      String url = "http://www.meineurl.de";

      try
      {
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost         
        httpPost   = new HttpPost( url );
        List nameValuePairs = new ArrayList(1); 
        nameValuePairs.add(new BasicNameValuePair("text", "processing"));
       
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        //println( "executing request: " + httpPost.getRequestLine() );
        HttpResponse response = httpClient.execute( httpPost );
        HttpEntity   entity   = response.getEntity();

        println("----------------------------------------");
        println( response.getStatusLine() );
        println("----------------------------------------");

        if( entity != null ) entity.writeTo( System.out );
        if( entity != null ) entity.consumeContent();

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpClient.getConnectionManager().shutdown();      

      } catch( Exception e ) { e.printStackTrace(); }
I don't know the Apache library, but I see the line:
entity.writeTo( System.out );
System.out outputs to the console. You can replace it probably with any OutputStream. I would use a ByteArrayOutputStream, then use toString() on it, after the writing.