HTTP Requests - Example

Paladin
Posts: 23
Joined: Fri Jan 13, 2017 8:45 pm

HTTP Requests - Example

Postby Paladin » Thu Apr 06, 2017 8:22 pm

I'm working from the HTTP_request_example, and it's not working exactly as I expected it to. Specifically, it's not printing the the HTTP response message to standard IO.

I'm testing via http://httpbin.org/get, and using both:

Code: Select all

$ curl http://httpbin.org/get

and Postman to send:

Code: Select all

GET /get HTTP/1.1
Host: httpbin.org
User-Agent: esp-idf/1.0 esp32
and in both cases I get the expected response:

Code: Select all

{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.51.0"
  }, 
  "origin": "73.0.241.158", 
  "url": "http://httpbin.org/get"
}
However, when I configure the ESP-IDF HTTP Request example to match:

Code: Select all

/* Constants that aren't configurable in menuconfig */
#define WEB_SERVER "httpbin.org"
#define WEB_PORT 80
#define WEB_URL "/get"
I also added an innocuous print statement to confirm the HTTP request that I think I'm sending is actually the one I'm sending with:

Code: Select all

printf("%s", REQUEST);
...which is inserted right before the request is written to the socket...
I get the following (abbreviated) output:

Code: Select all

 (2210) example: Connected to AP
I (2380) example: DNS lookup succeeded. IP=184.72.248.171
I (2380) example: ... allocated socket

I (2430) example: ... connected
GET /get HTTP/1.1
Host: httpbin.org
User-Agent: esp-idf/1.0 esp32

I (2430) example: ... socket send success
I (11180) wifi: pm start, type:0


At this point it just waits. But i'm expecting it to print the response message based on this snippet:

Code: Select all

/* Read HTTP response */
        do {
            bzero(recv_buf, sizeof(recv_buf));
            r = read(s, recv_buf, sizeof(recv_buf)-1);
            for(int i = 0; i < r; i++) {
                putchar(recv_buf[i]);
            }
        } while(r > 0);
So...where could I have gone wrong on this?

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: HTTP Requests - Example

Postby MalteJ » Thu Apr 06, 2017 9:19 pm

The example is not checking for the end of the HTTP response correctly.
A quick fix would be to replace HTTP/1.1 by HTTP/1.0 in the HTTP request.

Like this (line 49)

Code: Select all

static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\n"
Best,
Malte

Paladin
Posts: 23
Joined: Fri Jan 13, 2017 8:45 pm

Re: HTTP Requests - Example

Postby Paladin » Thu Apr 06, 2017 11:56 pm

I've tried that, but it did not fix it.

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: HTTP Requests - Example

Postby MalteJ » Fri Apr 07, 2017 9:54 am

also the new lines in the request must be \r\n instead of \n

like this:

Code: Select all

static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
    "Host: "WEB_SERVER"\r\n"
    "User-Agent: esp-idf/1.0 esp32\r\n"
    "\r\n";
If this doesn't help, can you paste your whole C file in a pastebin or gist? https://gist.github.com/

Best,
Malte

Paladin
Posts: 23
Joined: Fri Jan 13, 2017 8:45 pm

Re: HTTP Requests - Example

Postby Paladin » Fri Apr 07, 2017 11:31 am

Thanks. Adding the \r seems to have fixed it.

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: HTTP Requests - Example

Postby MalteJ » Sat Apr 08, 2017 12:15 am

Great! :)

I have created a pull request to fix this issue:
https://github.com/espressif/esp-idf/pull/500

Best,
Malte

saraedward
Posts: 1
Joined: Sat Apr 08, 2017 6:02 am

Re: HTTP Requests - Example

Postby saraedward » Sat Apr 08, 2017 6:08 am

(2210) example: Connected to AP
I (2380) example: DNS lookup succeeded. IP=184.72.248.171
I (2380) example: ... allocated socket

I (2430) example: ... connected
GET /get HTTP/1.1
Host: httpbin.org
User-Agent: esp-idf/1.0 esp32

I (2430) example: ... socket send success
I (11180) wifi: pm start, type:0
try to optimize this code

Who is online

Users browsing this forum: ESP_Roland, JosuGZ, Sweaps and 35 guests