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

}
3
  • 1
    what should it do and what does it? Shouldn't 'false' in json be in ""?
    – Juraj
    Jan 27 '19 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
    – Dkbay
    Jan 27 '19 at 19:23
  • it doesn't compile, not "doesn't work". use sendRequest("PUT", data.c_str());
    – Juraj
    Jan 27 '19 at 19:33
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.