11 years, 3 months ago.

How to define headers and data with the HTTPClient and a put request?

Hello,

I'm trying to get some data from my mbed to the cosm storage (the old pachube.com).

In order to accomplish this I would like to use the new EthernetInterface

Import libraryEthernetInterface

mbed IP library over Ethernet

and the corresponding HTTPClient.

Import libraryHTTPClient

A HTTP Client for the mbed networking libraries

In order to use to use the cosm API I have 2 options.

The 1st option is to create the following datapacket:

PUT /v2/feeds/<ID>/datastreams/<data>.csv HTTP/1.1
Host: api.cosm.com
Accept: */*
X-ApiKey: atUaYYu-ofNI6CY2O<  removed    >aTdCclFXb3FiTT0g
Content-Length: 2

<data>

The 2nd option is to greate the following datapacket:

PUT /v2/feeds/<ID>/datastreams/<data>.csv?key=atUaYYu-ofNI6CY2O<  removed    >aTdCclFXb3FiTT0g HTTP/1.1
Host: api.cosm.com
Accept: */*
Content-Length: 2

<data>

I tested both of the options with http://hurl.it and they work fine. However If I want to do this in the mbed I can only think of a way to implement the 2nd option. The code I use is:

#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"

EthernetInterface eth;
HTTPClient http;
char str[512];

int main() 
{
    eth.init(); //Use DHCP

    eth.connect();
    
    //PUT data
    strcpy(str, "99");
    HTTPText outText(str);
    HTTPText inText(str, 512);
    printf("\r\nTrying to put resource...\r\n");
    //int ret = http.put("http://httpbin.org/put", outText, &inText);
    int ret = http.put("http://api.cosm.com/v2/feeds/94832/datastreams/Test.csv?key=key=atUaYYu-ofNI6CY2O<  removed    >aTdCclFXb3FiTT0g", outText, &inText);
    if (!ret)
    {
      printf("Executed PUT successfully - read %d characters\r\n", strlen(str));
      printf("Result: %s\r\n", str);
    }
    else
    {
      printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
      printf("Result: %s\r\n", str);
    }
    
    
    eth.disconnect();  

    while(1) {
    }
}

However with this 2nd option the result is as follows:

Trying to put resource...
Error - ret = 1 - HTTP return code = 0
Result: 99

Is there someone who knows how to fix this?

1 Answer

Tim Bots
poster
11 years, 3 months ago.

Update: I got it working. The problem why solution 2 wasn't working is that at line 130 of "HTTPClient.cpp" the maximum size of the path part is defined at 64 characters. In my case I have 90 characters. Increasing this parameter to 100 fixes my problem.

However I'm still interested is there is a way to get solution 1 to work.

Accepted Answer

Hi can you send me the full working code.?

Thanks in advance...

posted by M.S. Arun 24 Feb 2015