arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
288 stars 172 forks source link

How to POST a request to a https-server #56

Closed bob3150 closed 5 years ago

bob3150 commented 5 years ago

Hello,

I trying around for a while to connect to a cloud server and do not get it working because I can only put In the address of the server without https:// - so I get back a HTTP-Statuscode of 301 (redirect). I also tried it with the https port 443 but the server is not open on port 443. Please help me to get my code working without the redirect. I need "https://api.server.com" instead of "api.server.com"

Rocketct commented 5 years ago

Hi @bob3150 could you share a minimal sketch to reproduce this issue? which Hardware/board are you using?

nhuzaa commented 5 years ago

Same issue on mine getting 301 while trying to request the https server

#define SERVER "test.com"
#define RESOURCE "/api/v1/gps-device"
#define PORT 80

void postRequest()
{

  String contentType = "application/x-www-form-urlencoded";

  Serial.print(F("Performing HTTP post request... "));
  int err = http.post(RESOURCE, contentType, datatosend);
  if (err != 0)
  {
    Serial.println(F("failed to connect"));
    delay(10000);
    return;
  }
  printOutput();
} 
Rocketct commented 5 years ago

Hi @nhuzaa which board are you using? i have tested with a wifi1010 and adding the certificate through the firmware uploader and change the port to 443. Have tried the post and the get, the results was:

this is my code:

/*
  Simple POST client for ArduinoHttpClient library
  Connects to server once every five seconds, sends a POST request
  and a request body

  created 14 Feb 2016
  by Tom Igoe

  this example is in the public domain
 */
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

char serverAddress[] = "www.test.com";  // server address
int port = 443;

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
String response;
int statusCode = 0;

void setup() {
  Serial.begin(9600);
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  Serial.println("making POST request");
  String contentType = "application/x-www-form-urlencoded";
  String postData = "";

  client.post("/api/v1/gps-device", contentType, postData);
  //client.get("/");

  // read the status code and body of the response
  statusCode = client.responseStatusCode();
  response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);
}
nhuzaa commented 5 years ago

@Rocketct its for board Modem: SIM808 R14.18

Try the server address to I changed it to avoid getting spammed. Its just give me 301 status error or returns -3 char serverAddress[] = "babunaninepal.com"; // server address

Rocketct commented 5 years ago

@nhuzaa could you share your complete sketch? i would like to understand if you are using a SSL client or not. The 301 problem is due to the fact that you are try to connect it without SSL, and setting the port to 80, i have reproduce it in this way, correct me if i'm wrong, could you try to add the SSL certificate to your device certificate list and try to make the post using the port 443?

nhuzaa commented 5 years ago

@Rocketct How do I add SSL certificate to the arduino device. I get -3 response upon requesting on port 443.

sandeepmistry commented 5 years ago

@nhuzaa please consulting the vendor for if the module and library for the SIM808 supports SSL.

For MKRGSM, you would pass in GSMSSLClient instead of a GSMClient.

Rocketct commented 5 years ago

closed due to lack of feedback, @nhuzaa @bob3150 if needed reopen the issue