Error 500 when trying a statuses/update via 1.1 API and JSON endpoint?

169 views
Skip to first unread message

Christophe Lauer

unread,
Jun 18, 2013, 11:49:09 AM6/18/13
to supertweet-...@googlegroups.com
Hi all,

I must have become kind of blind as I'm making circles for a couple of hours now trying to find out what I did wrong. I've tried on the command line using cURL and I know that the service and my account do work. It's just my cpp code that must be buggeous.

Here's what I capture on the output of the Arduino: 

Attempting to connect to SSID: frgre_valtechpublic
SSID: frgre_valtechpublic
IP Address: 172.20.0.167
signal strength (RSSI):-72 dBm
connecting to server...
making HTTP request...
HTTP/1.1 500 Internal Server Error

Date: Tue, 18 Jun 2013 15:34:28 GMT

Content-Type: application/json;charset=ISO-8859-1

Content-Length: 70

Connection: close

And now here's my full code, it is based on the standard Arduino sample "TwitterClient": 

#include <SPI.h>
#include <WiFi.h>

#define USERAGENT  "Arduino"     // user agent is the project name

char ssid[] = "frgre_valtechpublic"; //  your network SSID (name) 
char pass[] = "_WIFIPASSWORDHERE_";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS; // status of the wifi connection

// initialize the library instance:
WiFiClient client;

const unsigned long requestInterval = 60*1000;    // delay between requests; 30 seconds

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
char server[] = "api.supertweet.net";     // name address for twitter API

boolean requested;                     // whether you've made a request since connecting
unsigned long lastAttemptTime = 0;     // last time you connected to the server, in milliseconds

String currentLine = "";               // string to hold the text from server
String tweet = "";                     // string to hold the tweet
boolean readingTweet = false;          // if you're currently reading the tweet

void setup() {
  // reserve space for the strings:
  currentLine.reserve(2048);
  tweet.reserve(150);
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);  
 
    // wait 10 seconds for connection:
    delay(6000);
  } 
  // you're connected now, so print out the status:
  printWifiStatus();
  connectToServer();
}

void loop()
{
  if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar; 

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        Serial.println(currentLine);
        currentLine = "";
      } 
    }   
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and two minutes have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
  }
}

void connectToServer() {
  // I guess the problem must come from here, I'm unsure which input format is right for a status update
  // String tweetData = "{\"status\":%20\"Hello%20twitter%20from%20Arduino%20UNO!\"}";
  String tweetData = "status=Hello%20twitter%20from%20Arduino%20UNO";
  
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(server, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("POST /1.1/statuses/update.json HTTP/1.1");
    client.println("Host:api.supertweet.net");
    client.println("Authorization: Basic _MYBASE64CREDENTIALSHERE_");
    client.print("Content-Length: ");
    client.println(tweetData.length());
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.println("Connection:close");
    client.println("");
    client.println(tweetData);
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}   


void printWifiStatus() {
  // 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);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Thanks a mill for sharing your fresh eyes with me! ;)
Christophe

Mr Blog

unread,
Jun 18, 2013, 12:01:25 PM6/18/13
to supertweet-...@googlegroups.com

Not sure, but try added a space on the Host: header line:

    client.println("Host: api.supertweet.net");

I don't see anything from that IP address on the server(s) yet, so still looking.

Can you snoop the network or something to see the full response from the server?  What IP address handled your request? Where is the content/body of the response?

Christophe Lauer

unread,
Jun 18, 2013, 12:10:25 PM6/18/13
to supertweet-...@googlegroups.com
You're right, I need more tools and more details. 
I don't know how I could get full content for both request and response. Of course that would greatly help.
Going to DisplayMyIP.com from my iPhone on the same WiFi network returns 195.244.20.82 - maybe this can help you to find the request in the logs?

Mr Blog

unread,
Jun 18, 2013, 2:24:47 PM6/18/13
to supertweet-...@googlegroups.com
Did you try added the space on the Host: header?

Christophe Lauer

unread,
Jun 18, 2013, 6:03:22 PM6/18/13
to supertweet-...@googlegroups.com
Yes I did try it right away after you mentioned it, but that didn't help. If you have suggestions for tools that would help sniffing the network data, please let me know

Thx!

Christophe Lauer

unread,
Jun 20, 2013, 10:17:29 AM6/20/13
to supertweet-...@googlegroups.com
Could you share with me a sample code that publishes a status (code from whatever micro-controller platform) so that I can do a step by step check that my code is conform and I'm not missing any steps or parameters?

Thanks for you help!
Christophe

David Beckemeyer

unread,
Jun 20, 2013, 12:36:51 PM6/20/13
to supertweet-...@googlegroups.com
Here is some code from github (but I think this sample is using 1.0 API):

https://github.com/tinkerlog/TweetsOfWaste/blob/master/TweetsOfWaste/TweetsOfWaste.pde

Try adding a content type header:

client.println("Content-Type: application/x-www-form-urlencoded");
> --
> You received this message because you are subscribed to the Google Groups
> "SuperTweet MyAPI Proxy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to supertweet-myapi-...@googlegroups.com.
> To post to this group, send email to
> supertweet-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/supertweet-myapi-proxy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

David Beckemeyer

unread,
Jun 20, 2013, 12:46:11 PM6/20/13
to supertweet-...@googlegroups.com
Looking at the server logs, it appears there may be errors in your
BASE64 encoded credentials that are crashing HTTP at a pretty low
level, resulting in a 500 server error (which is probably technically
a bug in the server HTTP stack, but regardless).

Double and triple check the BASE64 encoded credentials string you are
using. Perhaps ensure it works from curl or something.

Christophe Lauer

unread,
Oct 10, 2013, 10:31:51 AM10/10/13
to supertweet-...@googlegroups.com
Hi David!

I did some tests and I noticed that my GET queries would work. I only got 500 error code when I was trying to publish a status via a POST through update.json. So I tried all possible things and I finally managed to find a sequence that works. 

I'm sharing this code with you now, may be this could help someone else in the community: 


    client.println("POST /1.1/statuses/update.json HTTP/1.1");
    client.println("Host: api.supertweet.net");
    client.println("Authorization: Basic __BASE64ENCODED__");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(tweetData.length());
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.println("");
    client.println(tweetData); 
    client.println("Connection: close");

THANKS FOR YOUR HELP, and thanks for this invaluable thing called SuperTweet! :)

HTH,
Regards,

Christophe Lauer



2013/6/20 David Beckemeyer <mrblog...@gmail.com>
You received this message because you are subscribed to a topic in the Google Groups "SuperTweet MyAPI Proxy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/supertweet-myapi-proxy/dPTY-G325zc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to supertweet-myapi-...@googlegroups.com.

To post to this group, send email to supertweet-...@googlegroups.com.
Visit this group at http://groups.google.com/group/supertweet-myapi-proxy.
For more options, visit https://groups.google.com/groups/opt_out.


Doug Biehl

unread,
Oct 28, 2013, 9:59:22 PM10/28/13
to supertweet-...@googlegroups.com
Christophe -

I've been struggling with the exact same POST issue via supertweet.net.  Would you mind posting your working code in its entirety?

Thanks!
Doug


2013/6/20 David Beckemeyer <mrblog...@gmail.com>

>> To post to this group, send email to
>> supertweet-...@googlegroups.com.
>> Visit this group at http://groups.google.com/group/supertweet-myapi-proxy.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>

--
You received this message because you are subscribed to a topic in the Google Groups "SuperTweet MyAPI Proxy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/supertweet-myapi-proxy/dPTY-G325zc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to supertweet-myapi-proxy+unsub...@googlegroups.com.

To post to this group, send email to supertweet-...@googlegroups.com.
Visit this group at http://groups.google.com/group/supertweet-myapi-proxy.
For more options, visit https://groups.google.com/groups/opt_out.


Mr Blog

unread,
Oct 30, 2013, 1:03:54 PM10/30/13
to supertweet-...@googlegroups.com
Look at the HTTP response for X-TwitterAPI-Status header. If present, then the 500 status was proxied from Twitter and the content should hopefully tell you something about why (usually 500 errors from Twitter are due to high load, or system maintenance). Otherwise, it's a Supertweet error and let us know the details, full HTTP request and HTTP response.

See:

Doug Biehl

unread,
Oct 30, 2013, 7:54:24 PM10/30/13
to supertweet-...@googlegroups.com
I determined that my problem was actually from client.connect() failing.  I set up a loop to check for the connection and retry on failure.  Works great now!

What I have working now is below.  I'm using a Duemilanove and an ethernet shield.


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


#define USERAGENT  "Arduino"     // user agent is the project name

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
char server[] = "api.supertweet.net";     // name address for twitter API

String currentLine = "";               // string to hold the text from server
String tweet = "";                     // string to hold the tweet
boolean readingTweet = false;          // if you're currently reading the tweet

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // Ethernet shield mac address
byte ip[] = { 192, 168, 1, 99 };                      // this is the ip within my lan

EthernetClient client;


void setup() {
  // reserve space for the strings:
  currentLine.reserve(2048);
  tweet.reserve(150);
 
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }
 
  Ethernet.begin(mac, ip);
  connectToServer();
}

void loop()
{
  // this piece reads the response (success, failure, etc.)

  if (client.connected()) {
    if (client.available()) {

      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar;

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        Serial.print(currentLine);
        currentLine = "";
      }
    }  
  }
}

void connectToServer() {
  String tweetData = "status=Another test of posting via @Arduino";
 
  // attempt to connect until successful

  Serial.println("connecting to server...");

  do {
    Serial.println("Attempting Connection...");
    client.connect(server, 80);
    Serial.print("    Connection attempt: ");
    Serial.println(client.connected());
  } while (client.connected()==0);


  // upon successful connection, transmit message
  if (client.connected()) {

    Serial.println("making HTTP request...");
    client.println("POST /1.1/statuses/update.json HTTP/1.1");
    client.println("Host: api.supertweet.net");
    client.println("Authorization: Basic __BASE64ENCODED__");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(tweetData.length());
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.println("");
    client.println(tweetData);
   
    Serial.println("completed");
    Serial.println("");
  }
}  
Reply all
Reply to author
Forward
0 new messages