A device on my network sends a http.post with a JSON body that I want my Photon to get and forward to an external web service.
I’m new to this, but I have the following code that I’ve cobbled together from examples of the forum and the httpclient library example app. It’s seeing the request, and sending a http.post to the external service ok, but I don’t understand how to properly read the content of the received JSON to put into my http.post request body.
#include "application.h"
#include "HttpClient.h"
#include "string.h"
/**
* Declaring the variables.
*/
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
TCPClient webClient;
TCPServer webServer = TCPServer(80);
String data;
char myIpAddress[24];
void setup() {
Spark.variable("ipAddress", myIpAddress, STRING);
IPAddress myIp = WiFi.localIP();
sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
webServer.begin();
}
void loop() {
if (webClient.connected() && webClient.available()) {
serveWebpage();
}
else {
webClient = webServer.available();
}
}
void serveWebpage() {
data="";
while (webClient.available())
{
data+=webClient.read();
}
webClient.println("<html>I got: "+data+"</html>\n\n");
webClient.flush();
webClient.stop();
// Request path and body can be set at runtime or at setup.
request.hostname = "example.com";
request.port = 80;
request.path = "/";
// The library also supports sending a body with your request:
request.body = "{\"data\":"+data+"}";
// Get request
http.post(request, response, headers);
delay(100);
}
The content of data
is currently just a list of numbers and not the content of the request I was expecting to see. What am I missing?