Http POST request with httpWebRequest

Hi everyone, I’m new to VVVV and VL but experienced in other languages (openFrameworks, Processing, Python). I’m trying to build an http POST request that should send a simple JSON to a webserver (which for now is on my local machine).

I looked at the examples and decided to build an asyncTask, which is perfect for HTTP calls. A simple GET, was easy to implement, however I’m struggling to find out how to add content to my POST call. This is my patch:

In order to add a string I used the GetRequestStream node and tried to write the content of an IOBox to the stream. However this doesn’t seem to work. The flow is identical to the one described here:

Also, note that the length of my string inside the asyncTask is 0, while outside it is correctly computed.
What am I doing wrong?

from a quick look at your patch, the order of things is not really clear. you see this yellow warning on the link, it tells you that you might violate the data flow paradigm since you are working with reference types and the order in which nodes get processed is not clear.

so if your types allow it, make a connection from the read node to the GetHttpResponse node so it will be executed after you did the write. of the other way around, depending on what you want to do.


Hi Tonfilm,
that’s what I thought I had to do, but when you use getRequestStream you obtain a Resource < stream > instance, which can’t be converted again in a webRequest instance. Hence it looks like there’s no way to fill the stream with data and feed it to the GetHttpResponse node.

However, looking at the C# example, looks like this is the way to do it:

WebRequest request = WebRequest.Create (" ");  
        // Set the Method property of the request to POST.  
        request.Method = "POST";  
        // Create POST data and convert it to a byte array.  
        string postData = "This is a test that posts this string to a Web server.";  
        byte[] byteArray = Encoding.UTF8.GetBytes (postData);  
        // Set the ContentType property of the WebRequest.  
        request.ContentType = "application/x-www-form-urlencoded";  
        // Set the ContentLength property of the WebRequest.  
        request.ContentLength = byteArray.Length;  
        **// Get the request stream.  
        Stream dataStream = request.GetRequestStream ();  
        // Write the data to the request stream.  
        dataStream.Write (byteArray, 0, byteArray.Length);  
        // Close the Stream object.  
        dataStream.Close ();
        // Get the response.  
        WebResponse response = request.GetResponse ();  
        // Display the status.  
        Console.WriteLine (((HttpWebResponse)response).StatusDescription);  
        // Get the stream containing content returned by the server.  
        dataStream = response.GetResponseStream ();  
        // Open the stream using a StreamReader for easy access.  
        StreamReader reader = new StreamReader (dataStream);  
        // Read the content.  
        string responseFromServer = reader.ReadToEnd ();  
        // Display the content.  
        Console.WriteLine (responseFromServer);  
        // Clean up the streams.  
        reader.Close ();  
        dataStream.Close ();  
        response.Close ();  

What is really missing in VL is the stream.close() function, but I guess it’s by design.

the c# code also looks a little outdated, nowadays you would use using code blocks that would manage the reader, dataStream and response. our Resource type has a similar functionality, it wraps resources that need to get disposed. you have access to the underlying resource via the Using region in category Resources… would that help?

but @Elias might have a better solution, i think he imported the node set and knows how it should be used.

Hi, Does this non-vl solution help?

but i would be also curious how to solve it in vl…

in cases where specifying the order of execution is not possible intuitively there is this workaround for now:
ie. use an if region with its condition set to true only to make sure that its content is executed before its input is passed on downstream.

having said that, i didn’t get it to work like this either.

frankly the whole webrequest stuff for vl was a bit too hastily imported. only later we discovered that it is obsolete and should now be done using HTTPClient which is all async itself already. so what we’re probably going to do, is marking all those webrequest nodes in vl obsolete as well and replace them with a more modern implementation based on the httpclient.

also sorting out the working with streams vs. Resources is still a todo on our list and will be simplified.

so please give us a moment to sort things…

1 Like

thanks everyone for the answers!

@tekcor that node works fine, it’s the one I’ll use in production.

@joreg HTTPCLient looks much simpler and easier to use, I’ll be watching out for updates. Generally speaking VVVV is a wonderful software, but my impression is that it’s a bit lacking on the web part (comprehensibly, since it’s focuses are multimedia and interaction). The JSON handling is a bit tricky, since you have to build the structure using XML related nodes, but I guess it’s also a matter of habit. Having said that, VL looks really promising and fun to use, at least from people coming from OOP like me.

By the way, does anyone know why the length of a string IObox is 0 inside an AsyncTask?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.