TCP Client not reading anything

@BDub I'm getting a '0' at the end of the read, is that what I should be getting?

Reading Facebook Data......
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: text/javascript; charset=UTF-8
ETag: "5d8d8fa86024882f78b93e63c06f69e3b44e81bf"
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Rev: 1100030
X-FB-Debug: 9rDexSyqdPgbWmPZ5O+FFCDeCOaQ4j1Q1F77B4D/TJQ=
Date: Wed, 29 Jan 2014 16:20:37 GMT
Transfer-Encoding: chunked
Connection: keep-alive

3f0
{"about":"Spark makes using and creating connected devices easy. Learn more and pre-order the Spark Core and accessories at www.spark.io","category":"Computers/technology","description":"Spark makes using and creating connected devices easy. The Spark Core an Arduino-compatible, Wi-Fi enabled development kit pairs with the Spark Cloud powered development platform to make creating internet-connected hardware a breeze now available for pre-order at: https://www.spark.io","is_published":true,"location":{"street":"","city":"Minneapolis","state":"MN","country":"United States","zip":""},"products":"Spark Core, Spark Cloud","talking_about_count":395,"username":"sparkdevices","website":"http://www.spark.io","were_here_count":0,"id":"427120970660412","name":"Spark","link":"http://www.facebook.com/sparkdevices","likes":2381,"cover":{"cover_id":537602636278911,"source":"http://sphotos-f.ak.fbcdn.net/hphotos-ak-prn1/s720x720/923349_537602636278911_656089984_n.jpg","offset_y":40,"offset_x":0}}
0

Yes that looks complete! What is that 0 or the 3f0 at the beginning?.. heck if I know. BTW I have a cool project with all of this code in mind… coming soon.

Try adding “Connection: close” to your request headers.

Does that get rid of the mystery ‘0’? What is that anyway?

The server is sending keep-alive connection data in pieces (Transfer-Encoding: chunked) and that “random” stuff is actually hexadecimal size of the next piece.

3f0 is 1008 in decimal and 0 means there is no more data coming this time.

Aha… makes sense… 1008 characters there. Except shouldn’t that be in the header as Content-Length: 1008 ? When I use Advanced Rest Client I see content-length: 574

Coffee’s code looked for "\r\n\r\n" to indicate the body. Is the first line of the body always the content length? I guess if so, we have an easy way to look for the end of the data, and we can close the socket ourselves.

Yeah, we are not really parsing the headers to understand how to read the data and handle the connection. I guess I have to go dig up some of my old HTTP browser code to see if I can ‘borrow’ from there. :smile:

HTTP Requests are quite annoying sometimes, because there is easy way to just parse the body you get with minumum headers sent and the harder way to make it work properly according the headers and stuff. With your own custom client you most of time get along with the easier one.

In this case, the trick to get Content-Length from Facebook is to add one more header “Accept-Encoding: deflate, compress”. Some servers seem to give you the content length without it.

Here is a link where you can check things. Easiest way is to use a proper client, check what headers it sends and receives and try to adapt your code to that. http://www.w3.org/Protocols/rfc2616/rfc2616.html

www.hurl.it is one of online request makers I have used lately, mainly because the short and easy url.

Ah, I see now. Thanks @weakset. The smaller content-length was due to it being compressed, and the additional headers request the data to be sent uncompressed. I see content-length of 1008 now in Advanced Rest Client, and also Hurl.it (cool site!).

It’s weird, just adding the “Accept-Encoding:” header with no value will decompress it… or you can force it to compress with “Accept-Encoding: gzip”.

In the case of the CC3000, it must be decompressing by default or we wouldn’t be able to read the characters. I wonder if that’s an automatic header sent, or just something it does if it sees compressed data.

Also to note, it is faster to receive compressed data, as long as you can uncompress it faster than sending it uncompressed. Probably a wash for small data packets.

Thanks!