I am trying to post a tweet using thingspeak but it is not getting posted. Am I missing something in my POST request or something wrong in my code?

#include <SPI.h>
#include <Ethernet.h>

const byte mac [] = {0x00, 0xC2, 0xA2, 0xE6, 0x3D, 0x57};
const int ledPin = 3;

const char webServer [] = "api.thingspeak.com";
const char webFile [] = "/apps/thingtweet/1/statuses/update";
const char apiKey [] = "xxxxxxxxxxxxxxxx";

EthernetClient myClient;

String msg = "This is my tweet";

void setup() {
  pinMode(ledPin, OUTPUT);

  Serial.println("Connecting to Network");

  if (Ethernet.begin(mac) != 1) {
    Serial.println("Connection Failed");

    while (true) {
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPin, LOW);
  } else {
    Serial.println("Connected to Network");

  // connect to thingstweet server
  Serial.print("Connecting to ");

  if (myClient.connect(webServer, 80) != 1) {
    Serial.println("Failed to connect to Thingspeak");
  } else {
    Serial.println("Sucessfully connected to ThingsSpeak");

  myClient.print("POST ");
  myClient.println(" HTTP/1.0");
  myClient.print("Host: ");
  myClient.println("Connection: close");
  myClient.println("Content-Type: application/x-www-form-urlencoded");
  myClient.print("Content-Length: ");
  myClient.println(msg.length() + 32);

  //POST some data


void loop() {
  // put your main code here, to run repeatedly:


Raw POST request from reqbin website

POST /apps/thingtweet/1/statuses/update HTTP/1.1
Host: api.thingspeak.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 48


Raw response from Reqbin

HTTP/1.1 200 OK
Date: Thu, 14 May 2020 21:24:11 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
X-Frame-Options: SAMEORIGIN
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: W/"6b86b273ff34fce19d6b804eff5a3f57"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: request_method=POST; path=/
X-Request-Id: f9afc383-63d3-4c91-8f15-4d8e4a90f87b
X-Runtime: 0.172584
X-Powered-By: Phusion Passenger 4.0.57
Server: nginx/1.9.3 + Phusion Passenger 4.0.57

  • is the arduino able to connect to the internet? – jsotola May 11 at 17:36
  • yes the arduino is connected to the network. Also it is able to connect to the Thingspeak – Zaffresky May 11 at 17:57
  • 1
    It's possibly because your Content-Length shouldn't be msg.length() + 32 but rather msg.length() + 34 due to the extra CRLF from myClient.println(msg);; that CRLF isn't needed though. If fixing that doesn't work, you can also try sending the POST manually with a site like this: reqbin.com/req/yjok4snr. To verify that your request is well-formed and that the server responds as expected. – SoreDakeNoKoto May 11 at 23:56
  • I have tried changing the content length but it didn't make any difference. Also by sending the POST request manually I am able to tweet. So I think the request is OK. But why doesn't it work with my arduino code? – Zaffresky May 12 at 15:08
  • When you tested manually, how exact did you get? Did you use the exact same headers, the exact same HTTP 1.0? Speaking of which, try changing that to HTTP 1.1, no reason to use 1.0, I think. Instead of fixing the changing the content length, get rid of the extra CRLF in your POST payload; make that print() instead of println(). And use the X-THINGSPEAKAPIKEY header in your request to specify your API key. Then update your question with the current sketch, if no combo of these works. – SoreDakeNoKoto May 13 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.