Electronic – Send REST PUT Commands from ESP8266

esp8266internetprogramming

I am interested in using an ESP8266 to power a physical interface for a network connected device. The device itself supports REST commands, but I am not quite sure how to send PUT requests from the ESP8266. Right now, I am able to use the device's web debugging interface to send REST commands, but I am not sure how I would translate that to the ESP8266.

Using a web browser, I am able to GET information by browsing to

<DEVICE_IP>/API/<USER_ID>/<RESOURCE>

but I can only get the current state of that resource by navigating to that URL. How would I send a PUT request?

Best Answer

After reading a short tutorial on the ESP8266 module using the Arduino API, I found that replacing the "GET" string with a "PUT" string with the command following. Specifically, to send the command

{"on": true}

to the resource at

/state

The ESP8266 would execute:

String command = "{\"on\": true}";
client.print(String("PUT ") + "/state" + "HTTP/1.1 \r\n" + 
             "Host: " + hosturl + "\r\n" +
             "Content-Length: " + command.length() + "\r\n" +
             "Content-Type: text/plain;charset=UTF-8\r\n\r\n" +
             command + "\r\n" +
             "Connection: close\r\n\r\n");

Honestly, I have very little idea why the "Host," "Content-Length," or "Content-Type" (or even "Connection") are there, but this isn't a MWE, just a WE.