Convert curl command to netcat?
Asked Answered
N

1

7

I am trying to convert a curl command (post) to a netcat command.

I already figured out how to GET/DELETE things like

curl -v http://localhost:1234/232

in netcat:

nc localhost 1234
GET 232
HOST: localhost

but I dont know how to POST something

For example: I want to save to value 2300 in my path 123

curl -v --data "val=2300" http://localhost:1234/123

and in netcat:
nc localhost 1234
POST 123
HOST: localhost

but where do i write my value?
Nodarse answered 2/1, 2015 at 16:18 Comment(1)
Pretty sure that should be GET /232 but your webserver might be tolerant...Midian
M
8
nc localhost 1234
POST /123 HTTP/1.0
Content-Length: 8
Content-Type: application/x-www-form-urlencoded
\n
val=2300

Content-Length is set to let the server know how much data you're going to send (string length of "val=2300"). Content-Type is to let the server know in which format the data is (form-encoded). The \n is the HTTP separation character (empty line) between headers and data.

Metropolitan answered 2/1, 2015 at 16:41 Comment(5)
ah cool. It worked :D. Can you explain me why content-lenght is 8 and why the type is application/x-www-form-urlencoded? and why do i have to add the \n?Nodarse
added some stuff by editing the post; curl will set those headers for you automatically based on the -d flag; you can check that with curl -vMetropolitan
last question: is "HOST: localhost" necessary? My teacher just gave me the code and I can type it without it. But when do I have to add it?Nodarse
see the accepted answer here #6004798 and notice the difference between HTTP 1.0 and HTTP 1.1Metropolitan
normally you should use nc -C to send CRLF instead of LF line endings. Never seen nc interpret \n, does it?Midian

© 2022 - 2024 — McMap. All rights reserved.