REST API error


(Eric) #1

I cannot figure out why I’m getting a HTTP 400 bad request error with the following code. The code below is being on an esp8266 microcontroller uploaded with the Arduino IDE. I have a test REST call that to another site that works perfectly fine. I’ve got an error somewhere in my call and I’m not seeing it.

Code getting HTTP 400 Error

#include <SPI.h>
#include <ESP8266WiFi.h>

WiFiClient client;

void getApiEndpoint()
{
  // Get the SmartThings endpoint
  if (client.connect("graph.api.smartthings.com", 80))
  {
    client.println("GET https://graph.api.smartthings.com/api/smartapps/endpoints");
    client.println("Host: https://graph.api.smartthings.com");
    client.println("Authorization: Bearer xxxx-xxxxx-xxxxxx-xxxxx-xxxxxxx");
    client.println("Accept: application/json");
    client.println();
    while (client.connected())
    {
      Serial.println("reading response");
      if (client.available())
      {
        String response = client.readStringUntil('\r');    // Read entire response
        Serial.println(response);
      }

    }
    client.stop();
  }
  else
  {
    Serial.println("not connected");
  }
}

Working code

void hitWebPage()
{
  if (client.connect("api.openweathermap.org", 80))
  {
    client.println("GET /data/2.5/weather?q=Manchester,uk&appid=xxxxxxxxxxxxx HTTP/1.0");
    client.println();
    while (client.connected())
    {
      Serial.println("reading response");
      if (client.available())
      {
        String description = client.readStringUntil('\r');    // Read entire response
        Serial.println(description);
      }
    }
    client.stop();
  }
  else
  {
    Serial.println("no client connect");
  }
}