[SOLVED] HTTP GET Request - How?

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

[SOLVED] HTTP GET Request - How?

Postby Stational » Sun Jul 03, 2016 11:27 pm

Hi,

i would like to know how i can send HTTP Get requests to a website. Sadly, the API Guide doesn't help :(

Could you give me a little code snippet about how this can be done? On the web i only found code for NodeMCU and for arduino but no SDK-specific code.

Help is very much appreciated :)

pratik

Re: HTTP GET Request - How?

Postby pratik » Mon Jul 04, 2016 12:58 pm

Hello Stational,

You will find almost everything in the examples, the AT command firmware code on Github/Espressif and the Code Snippets at
http://www.espressif.com/en/products/hardware/esp8266ex/resources

If you are familiar with the TCP/IP, you should be able to get the code running.
NOTE:
1. Use espconn_send() instead of espconn_sent(), which is discontinued.
2. Use GET request to access small pages first when you are testing your code, such as

Code: Select all

www.google.com/thispagedoesnotexist

That reduces possibility of memory overflow issues as well.

Hope it helps!

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

Re: [SOLVED] HTTP GET Request - How?

Postby Stational » Mon Jul 04, 2016 6:36 pm

Could you at least explain to me, where or how i have to define my request (such as: "http://www.mywebsite.org/myPHPFunction.php?myValue=5")?

Currently this is my definition:

Code: Select all

#define pheadbuffer "GET / HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: %s\r\nAccept: */*\r\n\r\n"
#define NET_DOMAIN "http://www.mywebsite.org/myPHPFunction.php?myValue=5"


At least i got something back from the server:

Code: Select all

<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4 mod_auth_passthrough/2.1 mod_wsgi/3.3 Python/2.7.1 Server at http://www.mywebsite.org/myPHPFunction.php?myValue=5 Port 80</address>
</body></html>


Edit: What i also don't understand is, why i have to create an array with the hardcoded IP:

Code: Select all

const char esp_tcp_server_ip[4] = {X, X, X, X};


if the IP comes from the DNS:

Code: Select all

espconn_gethostbyname(&user_tcp_conn,NET_DOMAIN,&tcp_server_ip,user_dns_found);
os_memcpy(user_tcp_conn.proto.tcp->remote_ip, esp_tcp_server_ip, 4);


If i don't type the IP of the server in that array i don't get any message back, instead i get an
reconnect callback, error code -11
even though i get the message "user_dns_found [correct IP of the server]".

EDIT:

Okay, now this works:

Code: Select all

#define pheadbuffer "GET /myPHPFunction.php?myValue=5 / HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: %s\r\nAccept: */*\r\n\r\n"

Finally !! :D

Who is online

Users browsing this forum: No registered users and 15 guests