arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
282 stars 170 forks source link

POST request with Basic Authentication #80

Closed yashfafola closed 4 years ago

yashfafola commented 4 years ago

I am trying to write data on the web app, via Arduino IoT 33 nano, using a POST request combined with basic authentication. But it is not working. GET request works perfectly and gets authentication from the web app.

http.beginRequest();
http.get("/");
http.sendBasicAuth(DB_USER, DB_PASS);
http.endRequest();

This works. but...

http.beginRequest();
http.post(PostUrl, "text/plain", PostBody);
http.sendBasicAuth(DB_USER, DB_PASS);
http.endRequest();

This doesn't work. PostUrl and PostBody are in a correct format, because the same POST request works with Postman and gets a success code. From the web app side, everything is running well, and verified via Postman. I want to know if there's something more needed for POST requests. Also there is no Arduino example given for POST request with Basic Authorization. The web app doesn't receive any authorization credentials.

yashfafola commented 4 years ago

Solved! Seems, the POST request should be implemented with custom headers. Not like the one I used in the code mentioned. Sequence: Post URL with method type, then pass the Authentication credentials, then data with its data type and data size with headers if any.

gomezramones commented 4 years ago

I'm having the same issue but I havent been able to solve it.

String contentType = "application/json";

  http.beginRequest();
  //http.post(url_auth,"text/plain", "");
  http.post("/api/auth/login","text/plain", "");
  http.sendHeader("username",user);
  http.sendHeader("password",password);

  http.endRequest();

  //http.beginRequest();
  //http.sendHeader("Authorization","Basic c2luYXBzaXNfdTpmNFgwXktpVjZKQlEzQSoy");
  //http.endRequest();

  int statusAuth = http.responseStatusCode();
  Serial.print("Status response:");
  Serial.println(statusAuth);

I always receive timeout.

yashfafola commented 4 years ago

I'm having the same issue but I havent been able to solve it.

String contentType = "application/json";

  http.beginRequest();
  //http.post(url_auth,"text/plain", "");
  http.post("/api/auth/login","text/plain", "");
  http.sendHeader("username",user);
  http.sendHeader("password",password);

  http.endRequest();

  //http.beginRequest();
  //http.sendHeader("Authorization","Basic c2luYXBzaXNfdTpmNFgwXktpVjZKQlEzQSoy");
  //http.endRequest();

  int statusAuth = http.responseStatusCode();
  Serial.print("Status response:");
  Serial.println(statusAuth);

I always receive timeout.

Try this.

      http.beginRequest();
      http.post("/api/auth/login");
      http.sendBasicAuth(user, password);
      http.sendHeader("Content-Type", "text/plain");
      http.sendHeader("Content-Length", PostBody.length()); // if any msg after authentication (PostBody is my msg)
      http.print(PostBody); // if any postbody
      http.endRequest();

This is working for me as basic authentication and posting to database.