sending txt file through http POST request

03 May 2011

Hello guys,

I am trying to send a txt file using a POST request with the HTTPClient library. I therefore use a HTTPFile as arguement for the post call. On the server, which gets the file, runs a php script, which grabs the filename using $_FILE['userfile]['name'] and processes the file. What happens right now is, "userfile can not be found". This array ($_FILE) actually doesn't exist. So I assume the http header is not good. I have to add some metadata that usually is given when you upload a file using a html form. The mBed request has to look to the server like a usual upload through such a form.

Does anybody know how to do this. I experimented around with the setRequestHeader() function of the HTTPClient class, but didn't get to anything. My code looks like this right now:

    HTTPFile *visitorTimestamps = new HTTPFile(file);
    twitter.setRequestHeader("Content-Type","multipart/form-data; boundary=boundary");
    twitter.setRequestHeader("Content-Disposition","form-data; name=\"userfile\"; filename=\"timestamps.txt\"");
    twitter.setRequestHaeder("Content-Type","text/plain");
    HTTPResult r = twitter.post( uri, *visitorTimestamps, NULL);
    if( r == HTTP_OK ){
        printf("Data sent with success!\n");
        visitorTimestamps->clear();
        return 1;
    }   
    else{
        printf("Problem occured when sending data, return code %d.\n", r);
        visitorTimestamps->clear(); 
        return 0; 
    }

I desperately need this thing done.

Thanks to anyone.

03 May 2011

OK, so far I know, I have to submit the "Content-type: multipart/form-data", but what does the HTTPClient itself put into the header? Does ist have a certain header? How can I catch the sent stuff?

Thanks guys! Cheers

03 May 2011

Hi Waldemar,

I also did update my php server with an POST command. Perhaps this can help you as a way to send your file.

    FILE *fp = fopen("/local/calib.txt", "r"); 
    if (fp==NULL) printf ("Error opening calib.txt file\n");

    fscanf(fp, "%d,%d,%d,%d,%d,%d,%d,", &LOG,&E1,&E2,&G1,&G2,&W1,&W2); 
    fscanf(fp, "%s\n", URL);
    fclose(fp);


        if (htmlflag) {  //every 5 minutes this htmlflag gets true
                       Gr=1;
                       htmlflag=false;
                       char data[16]={0};
                       HTTPClient myhttp;
                       HTTPMap msg;
                       snprintf(data, 16, "%d", packet_ID);
                       msg["t1"]=data;
                       snprintf(data, 16, "%d", Gas);
                       msg["t2"]=data;
                       snprintf(data, 16, "%d", Water);
                       msg["t3"]=data;
                       snprintf(data, 16, "%d", Elektriciteit);
                       msg["t4"]=data;
                       msg["t5"]="1013";
                       snprintf(data, 16, "%d", buitentemperatuur);
                       msg["t6"]=data;
                       snprintf(data, 16, "%d", binnentemperatuur);
                       msg["t7"]=data;
                       myhttp.basicAuth("xxxxxxx","xxxxxxxx");
                       HTTPResult r = myhttp.post(URL, msg, NULL);
                       
                       if (r == HTTP_OK)
                               { 
                                   printf("succesfull timestamp update\n");
                                   packet_ID++;
                               }
                               else
                               {
                                   printf("problem occured, returncode%d\n",r);
                               }

I hope this will work for your.

Kind regards, Henk Kalk

03 May 2011

Hello Henk,

thanks for that neat code snippet. I see that you are using HTTPMap, did you also try it using HTTPFile? I experiences, that it takes over two minutes with my setup to send 100 measured values through HTTP Post using HTTPMap over a UMTS connection. Using the same setup with HTTPFile it takes some seconds, god knows why...

Thanks for the post!

Cheers!

03 May 2011

OK, I now looked at some data traffic using Wireshark. This is what I got, when using a simple html form to upload some txt file:

POST /ex/fup.cgi HTTP/1.1
Host: cgi-lib.berkeley.edu
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://cgi-lib.berkeley.edu/ex/fup.html
Content-Type: multipart/form-data; boundary=---------------------------18736355632318
Content-Length: 288

-----------------------------18736355632318
Content-Disposition: form-data; name="upfile"; filename="test.txt"
Content-Type: text/plain

huhu
-----------------------------18736355632318
Content-Disposition: form-data; name="note"


-----------------------------18736355632318--

There's a lot of needless stuff, I guess. Also I had a closer look at the code, especially at http://mbed.org/users/segundo/libraries/NetServices/ljhqix/docs/HTTPClient_8cpp_source.html and found, that there is no boundary inserted automatically and also nothing after the data itself, which would also be the boundary. Tomorrow, I will sniff the actual POST request of my mBed and post it here.

Cheers!

04 May 2011

So this is what is beeing sent when using this code:

POST /buga/upload.php HTTP/1.1
Host: www.XXX.de
Content-Length: 0

then the server immediately sends this:

HTTP/1.1 200 OK
Date: Wed, 04 May 2011 08:36:00 GMT
Server: Apache/2.2.14
X-Powered-By: PHP/5.3.2-1ubuntu4.8
Vary: Accept-Encoding
Content-Length: 48
Content-Type: text/html

The file you attempted to upload is not allowed.

and now the mBed interrupts with this:

1280005527
1280005528
1280005529
1280005530
1280005531
1280005549
1280005550
1280005551
1280005552
1280005553

afterwards the server send a html page saying I can't do this. Now how could this ever work, when building the library? No server could ever accept this as usual POST message, could it?

I am trying to build up a nice header for this now.

12 May 2011

OK, guys, here we go again.

Now I funmbled around with the NetServicesSource library and chnaged HTTPClient and HTTPFile classes so they do the job. Next step is to test this till it gets no better. Will release changes on my profile later on.

Cheers

29 Oct 2016

Can you post an example of how to read a text file from a server?