1

I have a ESP-S3 board and am trying to get JSON data from our server. I am using the following code (see below) as a basis for mine.

This code works. If I change

http://jsonplaceholder.typicode.com/comments?id=10

to http://mywebsite.com

I do get the html for our server's main page.

If I then change

http://mywebsite.com

to

http://mywebsite.com/profiles

I get status code 200 but no JSON.

I type http://mywebsite.com/profiles in a browser it returns the JSON or if I use postman and do a GET on http://mywebsite.com/profiles it returns JSON.

It sounds like this issue posted a couple of years ago: esp32 http client response only 200 didn't receive the data after that

Any insight as to what I am doing wrong would be greatly appreciated.

#include <WiFi.h>
#include <HTTPClient.h>
 
const char* ssid = "yourNetworkName";
const char* password =  "yourPassword";
 
void setup() {
 
  Serial.begin(115200);
  delay(4000);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
 
}
 
void loop() {
 
  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
 
    HTTPClient http;
 
    http.begin("http://jsonplaceholder.typicode.com/comments?id=10"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request
 
    if (httpCode > 0) { //Check for the returning code
 
        String payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
      }
 
    else {
      Serial.println("Error on HTTP request");
    }
 
    http.end(); //Free the resources
  }
 
  delay(10000);
 
}
4
  • how big is the JSON response? There's not a huge amount of room for large responses. Try a smaller response and see if you still get nothing.
    – dandavis
    Commented Nov 18, 2022 at 20:05
  • I can't reproduce what you experienced, your code works perfectly and I got the json back. The only difference is that I'm using an ESP32 instead of ESP32S3. Maybe you try to swap the lines between String payload = http.getString(); and Serial.println(httpCode);, this might provided sufficient time for payload to be received completely.
    – hcheung
    Commented Nov 19, 2022 at 2:32
  • dandavis - that is definitely something to check. thank you for the suggestion. hcheung - the above code works for me too. It fails when I use my server.
    – BAW
    Commented Nov 21, 2022 at 13:09
  • dandavis - that was the issue. a smaller response size worked.
    – BAW
    Commented Nov 21, 2022 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.