Help with HTTP GET Command

In the example program, "BasicHttpClient", line 77 has the instruction "http.begin("http://example.com/index.html");" and line 81 has "int httpCode = http.GET();".

What is the relationship between these two lines of code? Does the "http.begin" load up the send data while the "http.GET();" sends the data on the network?

Do I need one "http.GET();" line for every "http.begin"?

Thanks for the help.

you can for example set HTTP headers between begin and get. get sends the request

Thank you Juraj. I programmed PLC's for 30+ years and have no experience with HTTP commands. Could you show me what a HTTP header looks like?

Which platform it is -- ESP8266 or ESP32S? Are you using local WiFi Network or direct WiFi connection between Server and Client?

I am using an ESP32-S3 over Wifi to a web controlled power strip. The power strip is hardwired to a Wifi mesh node.

Jurai - Thank you for your help. It's greatly appreciated.

One problem I am having is that I see more information in the header code than I can find in the product manual. I have a program working, I am not sure why. I am trying to control outlets on a network enabled power switch. The program attached toggles the state of outlet #5 every 5 seconds. I modified the BasicHttpClient example. How far off the mark am I?

/**
 * BasicHTTPClient.ino
 *
 *  Created on: 24.05.2015
 *
 */
#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

    bool flip = true;

void setup() {

    USE_SERIAL.begin(115200);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();
    
    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    wifiMulti.addAP("*****", "*****");

}

void loop() {
  
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        if (flip == false) {
          USE_SERIAL.print("[HTTP] begin...\n");
          // configure traged server and url
          //http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
          http.begin("http://192.168.39.93/index.htm"); //HTTP
          http.begin("http://admin:1234@192.168.39.93/outlet?5=OFF");
          flip = true;          
        }

        else {
          USE_SERIAL.print("[HTTP] begin...\n");
          // configure traged server and url
          //http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
          http.begin("http://192.168.39.93/index.htm"); //HTTP
          http.begin("http://admin:1234@192.168.39.93/outlet?5=ON");
          flip = false;          
        }

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();
 
        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(5000);
  
}

do you get some error?

No. The outlet toggles every 5sec as expected and I get:
"11:07:34.396 -> [HTTP] GET... code: 200"
as the response.

HTTP code 200 is success

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.