Forwarding get request with TCPClient, trouble reading received request data

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?

I guess your data+= operation doesn't really understand what you want that number webClient.read() provides it with to be used as - after all it's just a generic number.
You could try data += (char)webClient.read(); as a hint that you want that byte interpreted as a character and not as number.

But I'd rather advise against using String and better go for a character array to prevent possible issues with heap fragmentation if your program is meant to be running for more than just a few hours.

1 Like

Thanks for the heads-up on using char. That fixed it.

I have seen stability issues, where the Photon becomes unresponsive and goes off and back online, so perhaps my use of String is the cause. Although, I’m not sure how I can use a char array with the HttpClient library I’m using. I’ve seen other examples, which makes use of an older (and slower?) library, which just write print/println to the server, but this library seems to just have a single request, which is what prompted me to use a String.

A character array will be usable in virtually any place where you can use String - after all, the buffer used inside the String object is a char[].

1 Like

Ah, right - thanks. I was conflating two issues and confusing myself.