# Status Code -2 On HTTP Request

**URL:** <https://forum.arduino.cc/t/status-code-2-on-http-request/989008>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [May 6, 2022, 3:54pm UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008 "2022-05-06T15:54:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcoundon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rcoundon/32/1126393_2.png) [@rcoundon](https://forum.arduino.cc/u/rcoundon)\
**Post date:** [May 6, 2022, 3:54pm UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008/1 "2022-05-06T15:54:41Z")

</div>

I'm very new to Arduino ( 1 day in ) and it's 20 years since I wrote any C so go easy!  
Using an MKR1000 WiFi I'm creating a sketch to ultimately send a message to an IFTTT webhook based on a change in a magnetic switch . However, in the interim, to get things working I'm just trying to send an HTTP POST to a RequestBin endpoint.  
WiFi connects fine but whenever I make the HTTP request, I get status code -2 returned and no request hits RequestBin. Having hunted around on the internet for what -2 means I've come up empty.

This is what my code looks like:

```cpp
#include "arduino_secrets.h"
#include <ArduinoHttpClient.h>
#include <WiFi101.h>

const char* serverName = "somesubdomain.m.pipedream.net";

const char* serverPath = "/";
const int port = 443;
int status = WL_IDLE_STATUS;
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);

const int sensor = 4;
int state;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  pinMode(sensor, INPUT_PULLUP);
  
  while (status != WL_CONNECTED) {
    Serial.println("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.println("SSID: ");
  Serial.println(WiFi.SSID());

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

void loop() {
  //Check WiFi connection status
  if(WiFi.status() == WL_CONNECTED){
    
    int newState = digitalRead(sensor);
    Serial.println(String("newState: " + state));

    if(state == HIGH) {
      Serial.println("HIGH");
    }
    if(state == LOW) {
      Serial.println("LOW");
    }
    
    if(state == HIGH && newState == LOW){
      Serial.println("Closing door");
      client.post(serverPath, "application/json", "{\"value1\": \"Opening door\"}");
       
      int statusCode = client.responseStatusCode();
      String response = client.responseBody();
      client.stop();
    
      Serial.print("Status code: ");
      Serial.println(statusCode);
      Serial.print("Response: ");
      Serial.println(response);
    
    } else if(state == LOW && newState == HIGH) {
      Serial.println("Opening door");      
      client.post(serverPath, "application/json", "{\"value1\": \"Opening door\"}");
     
      int statusCode = client.responseStatusCode();
      String response = client.responseBody();
      client.stop();
      Serial.print("Status code: ");
      Serial.println(statusCode);
      Serial.print("Response: ");
      Serial.println(response);
    }
    state = newState;
  }
  delay(5000);
}

```

As always, any help or pointers greatly appreciated

---

<div class="post-metadata">

**Author:** ![rcoundon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rcoundon/32/1126393_2.png) [@rcoundon](https://forum.arduino.cc/u/rcoundon)\
**Post date:** [May 6, 2022, 5:27pm UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008/2 "2022-05-06T17:27:13Z")

</div>

I did come across this:

> [@HTTP status code "-3"](https://forum.arduino.cc/t/http-status-code-3/525020/2):
>
> Status -3 is timeout. The reason is probably that you promise 'len' bytes to the server, but you end the request before sending them. The server just keeps on waiting. Just follow the [example](https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/examples/PostWithHeaders/PostWithHeaders.ino). Pieter

Which states

> This call was made when the HttpClient class wasn't expecting it to be called. Usually indicates your code is using the class incorrectly

However I'm really just following the example so not sure why it's returning that status

---

<div class="post-metadata">

**Author:** ![rcoundon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rcoundon/32/1126393_2.png) [@rcoundon](https://forum.arduino.cc/u/rcoundon)\
**Post date:** [May 6, 2022, 6:01pm UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008/3 "2022-05-06T18:01:22Z")

</div>

Further to this, I've changed the code to the more verbose way of specifying the request and I get the same issue. However, if I switch from WiFiSSLClient to WiFiClient I actually get a response from the endpoint complaining that I've sent HTTP and not HTTPS

> 400 The plain HTTP request was sent to HTTPS port

So it appears this could be related to the WiFiSSLClient or its interoperation with the ArduinoHttpClient

For reference, code looks like (ignore that lack of DRYness - I'm just hacking at the moment):

```arduino
#include "arduino_secrets.h"
#include <ArduinoHttpClient.h>
#include <WiFi101.h>

const char serverName[] = "eo1buddftcclsc6.m.pipedream.net";
const char serverPath[] = "/";
const int port = 443;

int status = WL_IDLE_STATUS;
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);

const int sensor = 4;
const int STARTING_STATE = 42;
int state = STARTING_STATE;
String contentType = "application/json";

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  pinMode(sensor, INPUT_PULLUP);

  while (status != WL_CONNECTED) {
    Serial.println("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.println("SSID: ");
  Serial.println(WiFi.SSID());

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

void loop() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {

    int newState = digitalRead(sensor);
    Serial.println(String("newState: " + state));

    if (state == HIGH) {
      Serial.println("HIGH");
    }
    if (state == LOW) {
      Serial.println("LOW");
    }
    if (state != STARTING_STATE) {
      if (state == HIGH && newState == LOW) {
        String data = "{\"value1\": \"Closing door\"}";
        Serial.println("Closing door");
        client.beginRequest();
        client.post(serverPath);
        client.sendHeader("Content-Type", contentType);
        client.sendHeader("Content-Length", contentType.length());
        client.beginBody();
        client.print(data);
        client.endRequest();
        
        int statusCode = client.responseStatusCode();
        String response = client.responseBody();

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

      } else if (state == LOW && newState == HIGH) {
        Serial.println("Opening door");
        String data = "{\"value1\": \"Opening door\"}";
        Serial.println("Opening door");
        client.beginRequest();
        client.post(serverPath);
        client.sendHeader("Content-Type", contentType);
        client.sendHeader("Content-Length", contentType.length());
        client.beginBody();
        client.print(data);
        client.endRequest();
        
        int statusCode = client.responseStatusCode();
        String response = client.responseBody();
        
        Serial.print("Status code: ");
        Serial.println(statusCode);
        Serial.print("Response: ");
        Serial.println(response);
      }
    }
  state = newState;
  }
  
  delay(5000);
}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [May 7, 2022, 7:31am UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008/4 "2022-05-07T07:31:29Z")

</div>

You can dig through the libraries that you use; grep / grepWin are you friends. Search all .h and .cpp files for -2 to see what shows up.

---

<div class="post-metadata">

**Author:** ![rcoundon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rcoundon/32/1126393_2.png) [@rcoundon](https://forum.arduino.cc/u/rcoundon)\
**Post date:** [May 7, 2022, 7:50am UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008/5 "2022-05-07T07:50:53Z")

</div>

Hiya. Yeah, I found

This call was made when the HttpClient class wasn't expecting it to be called. Usually indicates your code is using the class incorrectly  
static const int HTTP\_ERROR\_API =-2;

Which sadly isn't very helpful!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2022, 7:51am UTC](https://forum.arduino.cc/t/status-code-2-on-http-request/989008/6 "2022-11-03T07:51:51Z")

</div>

Closed on November 3, 2022, 7:51am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
