Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lwp-request in shell: how to make POST request with body?

I use simple shell script to test some http server that process POST requests. Usually it looks like:

echo "param1=value1&param2=value2" | POST localhost/service

But now I want to pass also some json in POST body and that's where I missing point completely.

man POST and google did't help much also.

Seems it must be either very simple or completely impossible.

Thanks for help.

like image 953
Vlad Fedin Avatar asked Aug 09 '11 21:08

Vlad Fedin


People also ask

How do you do a post request on your body?

The body format is defined by the Content-Type header. When using a HTML FORM element with method="POST" , this is usually application/x-www-form-urlencoded . Another very common type is multipart/form-data if you use file uploads.

What does lwp request do?

This program can be used to send requests to WWW servers and your local file system. The request content for POST and PUT methods is read from stdin. The content of the response is printed on stdout. Error messages are printed on stderr.


2 Answers

Either I'm missing something, or you should do

$ echo -n '{"json":"data"}' | POST -c "application/json" 'http://localhost/service?param1=value1&param2=value2'

If you need to put those parameters not as GET, but as POST as well, then look up multipart form data.

like image 61
Alexander Gladysh Avatar answered Nov 15 '22 15:11

Alexander Gladysh


You probably need to pass content type using -c:

POST -c application/json
like image 35
ziya Avatar answered Nov 15 '22 16:11

ziya