HTTPClient - Interactive Matter - How to read server response from POST

Hey everyone … having a hard time with this issue. I am using the HTTPClient library from Interactive Matter, arduino UNO R3 and arduino wifi shield.

I am able to successfully post to the server and I’m assuming that the response gets stored in FILE* result, however I am unable to read result to get the api’s resposne.

Here is the code I am using:

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

char respStart[] = "[{";
char respEnd[] = "}]";
char *respStr;
char subbedStr[512];

char ssid[] = "RWSWireless"; //  your network SSID (name) 
char pass[] = "xxxxxxxxxxx";    // 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;
//IPAddress server(192,168,1,5); 
//char server[] = "api.rwsdev.net";

WiFiClient client;
byte server[] = { 74,208,213,187 };
#define ARDUTWEET_URI "/ardutweet/access/tweet.php"

char postData[] = "auth_id=874513a21c58d8852344&email=jason@rwsdev.net&cmd=mentions";
//String postData = "auth_id=874513a21c58d8852344&email=jason@rwsdev.net&cmd=tweet&status=Test post tweet from Arduino";

void setup() {
  Serial.begin(9600);
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();
}

void loop() {
  HTTPClientWiFi client("api.rwsdev.net",server);
  client.debug(1);
  http_client_parameter ardutweet_api_header[] = {
   
        { "Content-Type", "application/x-www-form-urlencoded"  }
        ,
        { NULL, NULL }
    };
      
  FILE* result = client.postURI(ARDUTWEET_URI,NULL,postData,ardutweet_api_header);
  
  char streami[140];
  fgets(streami,140,result);

    int returnCode = client.getLastReturnCode();
    
    if (result!=NULL) {
      client.closeStream(result);  // this is very important -- be sure to close the STREAM
      Serial.println(streami);
    } 
    else {
      Serial.println("failed to connect");
    }
    
    if (returnCode==200) {
      Serial.println("data uploaded");
    } 
    else {
      Serial.print("ERROR: Server returned ");
      Serial.println(returnCode);
    } 
}

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");
}
void parseResponse(String respStr) {
  
}

The response returned from the API is: [{“stamp”:“Sun Mar 10 15:14:31 +0000 2013”,“user”:“RWSDev”,“command”:“define”,“arg”:“testing”}]

any help on how to read the response from the API would be much appreciated.

Thanks in advance!

Jason

RWSDev: The response returned from the API is: [{"stamp":"Sun Mar 10 15:14:31 +0000 2013","user":"RWSDev","command":"define","arg":"testing"}]

any help on how to read the response from the API would be much appreciated.

Given that you are able to tell us what the response is, doesn't that imply you have [u]already[/u] read the response? Perhaps you mean something different by 'read the response'. Are you trying to extract values from the string, or something?

I know what the response is because I have also posted using curl in php and using the standard "client.println()" methods for the wifi shield ... I am logging all requests, success, fail, and responses to the log files on the API server and seeing that the request is made and the response is sent. I have also used the chrome app, Simple REST Client to make the post and seen the response actually come through as well as connecting to the server via raw telnet and manually posting the data.

The problem I am having is that when using the HTTPClient.h library, which saves the response to a stream, I do not know how to read that stream to get the response data to be able to do anything with it.

Ultimately I would like to parse our the json response string into separate vars but that should be easy enough if I could just read the stream lol.

Thanks, Jason

RWSDev: The problem I am having is that when using the HTTPClient.h library, which saves the response to a stream, I do not know how to read that stream to get the response data to be able to do anything with it.

I see that your call to postURI returns FILE* result which you read with fgets() and then print. Doesn't that return you the text of the HTTP response?

It does not...

This is what I get on Serial.print:

83S117u99c99c101e115s115s102f117u108l32 97a117u116t104h101e110n116t105i99c97a116t105i111o110n91[123{34"115s116t97a109m112p34"58:34"83S117u110n32 77M97a114r32 49148032 49153558:49152458:51349132 43+48048048048032 50248049151334"44,34"117u115s101e114r34"58:34"82R87W83S68D101e118v34"44,34"99c111o109m109m97a110n100d34"58:34"100d101e102f105i110n101e34"44,34"97a114r103g34"58:34"116t104h105i115s32 105i115s32 97a32 116t101e115s116t

if I do this:

char cc;
while((cc = fgetc(result)) != EOF)
{
Serial.print(cc);
}
      client.closeStream(result);  // this is very important -- be sure to close the STREAM

apache access log shows:

[11/Mar/2013:20:13:29 -0500] "POST /ardutweet/access/tweet.php HTTP/1.1" 200 398

The implementation looks like it tries to discard the HTTP response header before returning the stream to you. Do you have an example of a complete response including the headers? Maybe the response body really is empty.

However, since the examples all ignore the response it's also possible that feature simply doesn't work properly.