1

I've been trying to send a put request from my esp8266 for a while not but I can't figure out why it doesn't work and there are barely any posts about put requests online I keep getting errors around int httpCode = http.sendRequest("PUT", String(data));

Error log: https://gist.github.com/dkbay/ab3fedf5e89d841d25b24fb829df74ef

#include <ESP8266HTTPClient.h>
#define LED_BUILTIN 2
const char* ssid = "Some SSDI";
const char* password = "Some wifi password"; 
int wifiStatus;
void setup() {
 Serial.begin(115200);\
 pinMode(LED_BUILTIN, OUTPUT);
 delay(200);
 // We start by connecting to a WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Your are connecting to;");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 digitalWrite(LED_BUILTIN, LOW);
 delay(500);
 digitalWrite(LED_BUILTIN, HIGH);
 Serial.print(".");
 }
} 
void loop() {
 wifiStatus = WiFi.status();
 if(wifiStatus == WL_CONNECTED){
 HTTPClient http;
 http.begin("http://google.com/");
 http.addHeader("Content-Type", "application/json");
 String data = "{\"on\":false}";
 int httpCode = http.sendRequest("PUT", String(data));
 String payload = http.getString();
 Serial.println(httpCode);
 Serial.println(payload);
 http.end();
 Serial.println("");
 Serial.println("Your ESP is connected!");
 Serial.println("Your IP address is: ");
 Serial.println(WiFi.localIP()); 
 }
 else{
 Serial.println("");
 Serial.println("WiFi not connected");
 }
 delay(1000); // check for connection every once a second
}
asked Jan 27, 2019 at 12:09
3
  • 1
    what should it do and what does it? Shouldn't 'false' in json be in ""? Commented Jan 27, 2019 at 13:11
  • @Juraj It wasn't in quotes when I made it in node... It should send a "PUT" request to my hue lights to update them but instead it htrows this error gist.github.com/dkbay/ab3fedf5e89d841d25b24fb829df74ef Commented Jan 27, 2019 at 19:23
  • it doesn't compile, not "doesn't work". use sendRequest("PUT", data.c_str()); Commented Jan 27, 2019 at 19:33

1 Answer 1

1
HTTPClient http;
http.begin("http://google.com?on=false");
http.addHeader("Content-Type", "application/json");
int httpCode = http.sendRequest("PUT", "");
String payload = http.getString();

try with this code, if server .php, get data by $_REQUEST

answered Apr 14, 2020 at 5:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.