2

I am trying to make an HTTP request the with EtherCard library, then get the full response. Using the code from the examples, I'm only able to capture the headers, which are then abruptly cut off. The issue seems to be that I can't make the buffer big enough to store the data, but the data, hence why it's cut off. But it's only 292 bytes.

Here is another question I asked trying to understand what the example code was doing: What is happening in this C/Arduino code?

Here is the data I'm trying to GET: http://jsonplaceholder.typicode.com/posts/1

String response;
byte Ethernet::buffer[800];  // if i raise this to 1000, response will be blank

static void response_handler (byte status, word off, word len) {
  Serial.println("Response:");
  Ethernet::buffer[off + 400] = 0;  // if i raise 400 much higher, response will be blank
  response = String((char*) Ethernet::buffer + off);
  Serial.println(response);
}

See the comments above for what I've attempted.

Here is the output from the code above:

Response:
HTTP/1.1 404 Not Found
Date: Fri, 20 Jan 2017 12:15:19 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2
Connection: close
Set-Cookie: __cfduid=d9714bd94284b999ceb0e87bc91705d501484914519; expires=Sat, 20-Jan-18 12:15:19 GMT; path=/; domain=.typicode.com; HttpOnly
X-Powered-By: Express
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no

As you can see, it's not the complete data, only some of the headers.

Community
  • 1
  • 1
Noah
  • 4,601
  • 9
  • 39
  • 52
  • 1
    `String((char*) Ethernet::buffer + off)` looks wrong to me. Are you sure it should not be `String((char*) Ethernet::buffer)`? – NathanOliver Jan 20 '17 at 12:39
  • 2
    You are getting an error 404 back and the content is only 2 bytes (`Content-Length: 2`). You are doing something else wrong. – gre_gor Jan 20 '17 at 12:55
  • 1
    You're misinterpreting the symptoms - that's the entire content and your request is incorrect. You should get yourself an introduction to HTTP headers and error codes. – molbdnilo Jan 20 '17 at 13:13
  • Some good info on design REST APIs, HTTP status codes, etc: http://www.restapitutorial.com/httpstatuscodes.html – Derrick Jan 20 '17 at 23:33

1 Answers1

0

There are several problems here:

1) You get a HTTP 404 response, which means the resource was not found on the server. So you need to check your request.

2) You are cutting off the string at pos 400:

Ethernet::buffer[off + 400] = 0;  // if i raise 400 much higher, response will be blank

That's why it stops after Cache-Control: no, which is exactly 400 bytes (byte 0-399).

You probably want Ethernet::buffer[off + len] = 0;, but you also need to check if that is not out of bounds (i.e. larger than your buffer size - that's probably why you get a 'blank' response).

For example, a 404 response from that server looks like this:

HTTP/1.1 404 Not Found
Date: Mon, 23 Jan 2017 07:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2
Connection: keep-alive
x-powered-by: Express
Vary: Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
x-content-type-options: nosniff
Etag: W/"2-mZFLkyvTelC5g8XnyQrpOw"
Via: 1.1 vegur
CF-Cache-Status: MISS
Server: cloudflare-nginx
CF-RAY: 32595301c275445d-xxx

{}

and the 200 response headers (from a browser):

HTTP/1.1 200 OK
Date: Mon, 23 Jan 2017 07:00:00 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
x-powered-by: Express
Vary: Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: public, max-age=14400
Pragma: no-cache
Expires: Mon, 23 Jan 2017 10:59:01 GMT
x-content-type-options: nosniff
Etag: W/"124-yv65LoT2uMHrpn06wNpAcQ"
Via: 1.1 vegur
CF-Cache-Status: HIT
Server: cloudflare-nginx
CF-RAY: 32595c4ff39b445d-xxx
Content-Encoding: gzip

So your buffer needs to be big enough to hold both the response headers and the data.

3) In the 200 response we see 2 things: the transfer is chunked, and gzipped (but the latter only happens when there is a Accept-Encoding: gzip header in the request.

The easiest way to handle this is to send a HTTP/1.0 request instead of HTTP/1.1 (chunked transfer and gzip are not allowed/available in HTTP/1.0).

Danny_ds
  • 11,201
  • 1
  • 24
  • 46