HTTP REQUEST via ENC28J60 ethercard.h (Arduino Mega2560) - arduino

We would like to send a http request with ethercard.h.
But when we doing this
ether.browseUrl(PSTR("GET /"), "", website, my_callback);
We get a 400 bad request answer.
We would like to test http request like:
-POST
-HEAD
-PUT
-DELETE
-TRACE
-OPTIONS
But it seems only this is working:
ether.browseUrl(PSTR("/"), "", website, my_callback);
but why?

For GET request you don't need to make a special definition. All requests are GET in default. That's why your request works without any definition. So i can share with you a POST request example. You can proceed from here:
Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"\r\n"
"$H"),
website, PSTR(PATH), website, stash.size(), sd);
It even has a link to its explanation. POST Request Example

Related

HTTP Host Field with empty value, by response 400

when executed this command:
curl -H "Host:" http://127.0.0.1
it response 400 Bad request.
from http rfc:
If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value.
why?
Because -H "Host:" removes the header altogether. See the docs for -H, --header <header/#file>:
[...] Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:". If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".
You need -H "Host;" to send an empty host header.

GET http request from Microcontroller to server (API)

I'm working with a stm32f4 microcontroller using lwip/stack, i use it to controle send http requests via ethernet .
the following code works fine:
sprintf(buffer, "GET /api/callAction?deviceID=80&name=turnOn\r\n");
strcat(buffer, "Host: 192.168.2.7\r\n");
strcat(buffer, "Connection: close\r\n");
strcat(buffer, "\r\n");
the problem is when the sever needs authentication like this :
admin:admin#192.168.2.7/api/callAction?deviceID=80&name=turnOn
i have tried adding an authorization part to the code :
strcat(buffer, "Host: admin:admin#192.168.2.7\r\n");
But the http request doesn't work .
Any ideas?
ps: im using Keil ARM /stm32f4 / lwip stack
Server: Fibaro home center lite
Have a read here:
https://en.wikipedia.org/wiki/Basic_access_authentication
you want to pass this string
Authorization: Basic XXXXXXXXXXXXXXXX
where XXXXXXXXXXXXXXXX is the Base64 version of username:password
for example, if username is Aladdin and password is OpenSesame then you have to Base64 encode the string Aladdin:OpenSesame which results into QWxhZGRpbjpPcGVuU2VzYW1l
you string is then:
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
Just strcat it as you do for all the other stuff:
strcat(buffer, "Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l\r\n");

How do I POST form data with UTF-8 encoding by using curl?

I would like to POST (send) some form data to a webserver using cURL on a terminal-prompt.
This is what I got so far:
curl --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod
The problem is that the umlaute ("äöü") are replaced by "?" when I receive the post request on the server.
I think I need to use an UTF-8 encoding for the POST request.
Does anybody know how I can achieve this?
You CAN use UTF-8 in the POST request, all you need is to specify the charset in your request.
You should use this request:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod

Is this a valid HTTP response?

I am writing a web server using C++, which responds the following for all requests:
static std::string rsp[] = {
"HTTP/1.1 200 OK\r\n",
"Server: WebServer\r\n",
"Content-Type: text/html\r\n",
"Content-Length: 3\r\n",
"Connection: close\r\n",
"\r\n",
"123"
};
the content "123" can be successfully shown in browser. But when I use apache-ab to do a test, ab always show errors like this:
ab -n 1 -c 1 http://127.0.0.1:1080/
apr_socket_recv: Connection reset by peer (104)
I thought that I'm closing the socket too quickly, so I commented the close() function. But ab just hold, ab seems to be waiting for a complete response.
If you can correctly render the response (123) in your browser, then it means that there is nothing wrong with your server, but with the request ab is sending to your server is not understood by your server.
ab's requests are not necessarily the same as a web browser's requests. And ab is not fully http 1.1 complainant, it's a http1.0 client.
"It (ab) does not implement HTTP/1.x fully; only accepts some 'expected'
forms of responses.
Source
Try this:
0. See the request being sent to your server: ab -n 5 -c 5 -v 10 http://127.0.0.1:8000/ using the verbosity argument. You should see something like this:
GET / HTTP/1.0
Host: 127.0.0.1:8000
User-Agent: ApacheBench/2.3
Accept: */*
Using firebug or something, get the requests being sent from your browser.
Now, match them, see what is the difference in the requests being sent. Use ab -H to send those custom headers to your web server from ab.

Correct http response to video stream

I'm currently developing my own little http server for video streaming, and i can't for the life of me figure out how this actually works...
This is the request i get from the client:
"GET / HTTP/1.1
Host: 127.0.0.1:8080
Accept: /
User-Agent: QuickTime.7.6.6 (qtver=7.6.6;cpu=IA32;os=Mac 10.6.4)
Connection: close
"
To which my http server responds(actual code):
response << "HTTP / 1.1 200 OK" << "\r\n"
<< "Accept-Ranges: bytes" << "\r\n"
<< "Connection: close" << "\r\n"
<< "Content-Type: video/x-msvideo" << "\r\n"
<< "\r\n";
followed by the actual video as a bytearray.
The video doesnt play... What am I doing wrong?
Two debugging suggestions:
Telnet directly to your webserver (e.g. telnet 127.0.0.1 8080) and type in the get request manually. Verify that the response you receive back are like you expect. This might cause your terminal settings/display in the window you run telnet to be messed up, but it is a very quick and simple test.
You can also capture the traffic with wireshark.

Resources