Max HTTP methods?
Hello there.
I have an EigenHarp, which talks to Max over OSC. No problems there. However, to change the LED colours on the instrument, I need to send http commands to the instrument.
For instance, to make one of the lights turn green, I need to send a PUT command to http://localhost:10250/row/%d/column/%d green , where the two %d's of course specify a certain row and column. And to turn a light off, I need to send a DELETE command in a similar way to http://localhost:10250/row/%d/column/%d
Now, I imagine this being possible using tools such as cURL, but there doesn't exist a Max implementation of that. Basically, my question is:
How can I send GET/PUT/DELETE http commands using Max?
Hi,
you can use [sadam.tcpClient]
for that purpose (see https://cycling74.com/forums/announce-the-sadam-library-version-2012-10-08 ).
HTH,
Ádám
Thank you very much! It looks like I can use this.
However, I do have a question. I cannot seem to figure out how I have to format the strings that I send to the sadam.tcpClient.
As outlined in the help file, I use a message connected to an [atoi]
and then through [append 13 10]
(Is this a carry return?), and then send that into the sadam.tcpClient
.
With the HEAD statement that's used in the HELP file it works fine. However, I believe the syntax for the DELETE and the PUT commands is different.
The DELETE command: Works, but deletes the lights on all the keys instead of only the one at a certain row/column. This is what should happen if I send a DELETE command to the main localhost:10250
(without a certain suburl).
The PUT command doesn't work at all. Probably because I need some extra syntax to show what value I'm uploading.
So,
Question 1: What is the syntax I need to use in the message when using a PUT command, and what for a DELETE command?
Question 2: How can I send messages to a specific URL on the port, i.e. to 127.0.0.1:10250/column/1/row/2
, instead to just 127.0.0.1:390
?
Thanks,
~Wiebe Marten
Hi,
I have to admit that I'm not an expert on HTTP syntax, therefore I don't have a solution for you right now. My TCP client is a generic TCP object that you can use for HTTP just as well as for any other protocol that requires a bi-directional connection. As you figured out, the input of the object are the bytes themselves that you want to send, which is the reason for having the [atoi]
after any textual command. The 13 10 is indeed a carriage return and it is needed by the HTTP HEAD command.
I already figured out how HTTP GET command works in another thread (https://cycling74.com/forums/download-text-from-wikipedia), this is the final version of the example patch on that thread that would download a webpage from Wikipedia and return the HTML document following the tag:
Although this is for HTTP GET, it might give you an idea on how HTTP PUT and/or HTTP DELETE should be implemented. I'll also look into the problem (I want to learn the tricks myself, too), but I can't promise that it would happen anytime soon.
Hope this helps,
Ádám
P.S. Regarding how you can send messages to a specific URL: I guess this should be part of the appropriate syntax for the mentioned HTTP commands. If you check my example, the actual URL is part of the HTTP GET command, while you still have to connect to the main Wikipedia server.
Wiebe,
I wasn't aware of Siska's TCP object when I originally implemented the REST object. I think I'm going to revisit that piece now so unless you're in a real hurry, I'll try and make this work over the next week or so using his objects. On te other hand, if you are in a hurry and you get it working sooner, maybe you woud post your patcher on the max4eigenharp site
D
Hello Dhjdhj, Siska,
I have been able to implement the DELETE request. The syntax is in fact similar to the GET request, in that it is:DELETE /relative/or/absolute/url/ HTTP/1.1
The PUT method seems to be harder, because you need to add a body to the message. I haven't figured out how to send a correct body yet(that the EigenHarp can understand as a colour).
It should be something along the lines of:
PUT /relative/or/absolute/url/ HTTP/1.1
body
With the being actual carriage-return and linefeed characters (i.e. in ASCII: 13 10 ).
However, this does not work. I'm not sure if my body format is incorrect, or that I need to add a Content-Type: header or something to the request to make it work.
Dhjdhj: It is not that I'm in a hurry, but I'd love to get this working. Of course I'd be glad to share the code with you, if I succeed.
Thanks a lot for your time,
Wiebe Marten
Allright, my progress so far:
"PUT /column/1/row/6 HTTP/1.1
Accept: */*
Content-Length: 5
Content-Type: application/x-www-form-urlencoded
green"
This message works. Hooray!
However, what I don't understand is how the messages in Max work when using multi-lines and quotes ("). The quotes seem to keep the intact, but they break things like the variables ($1 to $9). This means I have yet to figure out how to make it non-static(i.e. you specify the key number and color, and it will update the key for you), because I have no idea how to do that without using the variables.
Wiebe Marten
EDIT: My progress so far:
It looks like someone already responded to you on Stackoverflow. I don't have my eigenharp handy but looking at Siska's code, I put together the following abstraction (and test case) which looks like it sends out the right information, at least according to what is received by using the 'nc' command.
It's a real hack with spaghetti wiring but it might show the way forward.
Test case
PutRequest
MakeHttpHeader
Wiebe, the example I posted can be easily modified to make those parameters configurable which should solve the problem. Things like the length of the string are calculated automatically. If you have time to try it out, I'd be interested. I'm not currently in a position to test it with a real eigenharp until the weekend.
Hi Wiebe Marten,
What I usually do in my own projects is, that I try to keep everything as bytes and not as messages. This is a good way to avoid the question of how Max handles specific messages. For example, if you send the multi-line PUT message in your original patch to an [atoi]
object, you'll see that the line breaks are coded as 10 instead of 13 10 (at least on a Mac), while the standard line ending for HTTP is 13 10 (as a matter of fact, some servers support the CR and the LF and the CRLF and LFCR line endings as well, but this is not guaranteed by the standard). In this specific case, I would send the PUT message as this simple example shows:
Hope this helps,
Ádám
P.S: My first name is Ádám. Feel free to call me that way.
Hi again,
this is a corrected version of the above patch, where you can set the column and row data, and also the 'content-length' will be computed according to the colour:
HTH,
Ádám
Thank you so much for making my day, Ádám. This works wonderful!
I'll keep you posted on updates.
Have a great day,
Wiebe Marten