-->
Page 1 of 1

Airtable API and ESP8266

PostPosted: Mon Aug 31, 2020 4:26 am
by Jan Viñas
I'm trying to create a new record on Airtable using their API, from an ESP8266, but I keep getting error 301 (moved permanently). I've tried to make the request from reqbin.com and it works perfectly, returning 200 and creating the record. What am I doing wrong?

Reqbin request (works perfectly):

Code: Select allPOST /v0/appUpfgdBWvaXBwCN/Table%201 HTTP/1.1
Host: api.airtable.com
User-Agent: ESP8266HTTPClient
Connection: close
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
Content-Type: application/json
Authorization: Bearer ***
Content-Length: 61

{"fields":{"Temperature":25,"Humidity":50,"Pressure":101300}}


Arduino IDE code that I made for testing purposes:

Code: Select all#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"

#define DEBUG_ESP_HTTP_CLIENT

const char* ssid = "***";
const char* password = "***";

void setup() {
 
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");
  delay(1000);
}

void loop() {

  String data = "{\"fields\":{\"Temperature\":25,\"Humidity\":50,\"Pressure\":101300}}";
 
  HTTPClient airtable;
  airtable.begin("http://api.airtable.com/v0/appUpfgdBWvaXBwCN/Table%201");
  airtable.addHeader("Content-Type", "application/json");
  airtable.addHeader("Authorization", "Bearer ***");
  Serial.println("Sending HTTP Request");
  int httpResponseCode = airtable.POST(data);
  Serial.println("HTTP Request sent");
  if(httpResponseCode>0){
    String response = airtable.getString();   
 
    Serial.println(httpResponseCode);
    Serial.println(response); 
  }else{
    Serial.print("Error on sending POST Request: ");
    Serial.println(httpResponseCode);
  }
 
  airtable.end();

  delay(5000);
}



COM port log from ESP8266:
Code: Select all11:20:23.401 -> Connecting to WiFi..
11:20:23.605 -> ip:192.168.0.23,mask:255.255.255.0,gw:192.168.0.1
11:20:24.423 -> Connecting to WiFi..
11:20:24.423 -> Connected to the WiFi network
11:20:25.434 -> [HTTP-Client][begin] url: http://api.airtable.com/v0/appUpfgdBWvaXBwCN/Table%201
11:20:25.434 -> [HTTP-Client][begin] host: api.airtable.com port: 80 url: /v0/appUpfgdBWvaXBwCN/Table%201
11:20:25.434 -> Sending HTTP Request
11:20:25.567 -> [HTTP-Client] connected to api.airtable.com:80
11:20:25.600 -> [HTTP-Client] sending request header
11:20:25.600 -> -----
11:20:25.600 -> POST /v0/appUpfgdBWvaXBwCN/Table%201 HTTP/1.1
11:20:25.600 -> Host: api.airtable.com
11:20:25.600 -> User-Agent: ESP8266HTTPClient
11:20:25.600 -> Connection: close
11:20:25.600 -> Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
11:20:25.600 -> Content-Type: application/json
11:20:25.600 -> Authorization: Bearer ***
11:20:25.600 -> Content-Length: 61
11:20:25.600 ->
11:20:25.600 -> -----
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'HTTP/1.1 301 Moved Permanently'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'Content-Type: text/html'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'Date: Mon, 31 Aug 2020 09:20:25 GMT'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'Location: https://api.airtable.com/v0/appUpfgdBWvaXBwCN/Table%201'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'Server: Tengine'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'Content-Length: 278'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: 'Connection: Close'
11:20:25.735 -> [HTTP-Client][handleHeaderResponse] RX: ''
11:20:25.770 -> [HTTP-Client][handleHeaderResponse] code: 301
11:20:25.770 -> [HTTP-Client][handleHeaderResponse] size: 278
11:20:25.770 -> HTTP Request sent
11:20:25.770 -> [HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: 278).
11:20:25.770 -> [HTTP-Client][end] tcp is closed
11:20:25.770 -> 301
11:20:25.770 -> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
11:20:25.770 -> <html>
11:20:25.770 -> <head><title>301 Moved Permanently</title></head>
11:20:25.770 -> <body bgcolor="white">
11:20:25.770 -> <h1>301 Moved Permanently</h1>
11:20:25.770 -> <p>The requested resource has been assigned a new permanent URI.</p>
11:20:25.770 -> <hr/>Powered by Tengine</body>
11:20:25.770 -> </html>
11:20:25.770 ->
11:20:25.770 -> [HTTP-Client][end] tcp is closed


I censored WIFi information and the API key, so that's not what's causing the problem.

Thanks in advance and sorry for my bad English.

Re: Airtable API and ESP8266

PostPosted: Tue Sep 01, 2020 2:44 am
by schufti
to me it looks like it gets redirected to an https:// url and your esp8266 client is not prepared to handle redirection and/or https://

Re: Airtable API and ESP8266

PostPosted: Tue Sep 01, 2020 4:20 am
by Jan Viñas
This fixed my problem, directly making an HTTPS request works perfectly. Thanks!

Re: Airtable API and ESP8266

PostPosted: Wed Feb 10, 2021 6:19 am
by pradipkamu
Jan Viñas wrote:This fixed my problem, directly making an HTTPS request works perfectly. Thanks!



Can you send me the working code ....... :P :P :P :P :P