0

Nodemcu device is returning HTTP response -1 for a POST request on django rest api.

I tried the below http request to Postman, and it works perfectly fine.

http://localhost:8000/api/datalogs/?plant=http://localhost:8000/api/plants/1/&light=23&temperature=24&humidity=75&soil=Dry&moisture=787&remarks=/

This is my arduino post request:

if(WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    http.begin(host_url);

    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    httpCode = http.POST(postData);

    payload = http.getString();
    http.end();
}

The expected result is 200, but I get -1. Can someone please explain?

  • the values of host_url and postData are not shown ... your code is sending unknown data .. therefore there is nothing to explain – jsotola Sep 3 at 3:36
  • you use the name "localhost" in the request from other device? – Juraj Sep 3 at 5:12
  • can you be more specific about the code on nodemcu side like what is the host_url and also postdata. At the same time update the code form Django side like what function is catching the http response from nodemcu. –  Tarun Sep 11 at 4:27
  • Yes, thanks. I might be messing it with localhost, whereas my device has a different ip than my computer.. I should give it a try first Juraj – Tanveer Hoque Sep 11 at 20:20
  • The host_url is http://localhost:8000 and the postData is "plant="+String(host)+"plants/1/&light="+String(lux)+"&temperature="+String(temperature)+"&humidity="+String(humidity)+"&soil="+mRes+"&moisture="+mVal+"&remarks=/"; I think the issue might be localhost jsotola – Tanveer Hoque Sep 11 at 20:22

Your Answer

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

Browse other questions tagged or ask your own question.