I’m trying to send some a POST packet with a JSON-RPC payload. Trouble is, I’m new to it, and I can’t find a decent reference for structure, code, and other stuff.
I need to send a packet with these values inside to an {ip address}/sony/camera
{
"method": "actTakePicture",
"params": [],
"id": 1,
"version": "1.0"
}
Here’s the code I’m working on:
client.connect(ip,64321);
{
Serial.println("Connected to camera");
//Encodes JSON Data so that the camera will understand it.
//Also adds POST data and sends packet
client.println("POST /camera HTTP//1.1");
client.println("Host: " + webAddress + "//sony");
client.println("Accept: application/json");
client.print("Content-Length: ");
client.println(strlen(request));
client.println("Content-Type: application/json-rpc");
client.println("{/"method/":/"actTakePicture/",/"params/":/"[]/",/"id/": 1,/"version/": /"1.0/"}");
}
I’ve got some questions I need to know before I can send the thing. How do I encode the JSON parameters? Do I need to calculate the length of the packet, or is that handled by the TCP library?
How much of the above stuff do I need? I’m just not sure what I need to send, and am thus having trouble coding for it.
Thank you!
EDIT: Here is an encoding function that I found online that might be required
String URLEncode(const char* msg)
{
const char *hex = "0123456789abcdef";
String encodedMsg = "";
while (*msg!='\0'){
if( ('a' <= *msg && *msg <= 'z')
|| ('A' <= *msg && *msg <= 'Z')
|| ('0' <= *msg && *msg <= '9') ) {
encodedMsg += *msg;
} else {
encodedMsg += '%';
encodedMsg += hex[*msg >> 4];
encodedMsg += hex[*msg & 15];
}
msg++;
}
return encodedMsg;
}