Post request with Wget?
Wget currently doesn't not support "multipart/form-data" data. --post-file
is not for transmitting files as form attachments, it expects data with the form: key=value&otherkey=example
. It is actually possible to post other formats (json) if you send the corresponding header.
--post-data
and --post-file
work the same way: the only difference is that --post-data
allows you to specify the data in the command line, while --post-file
allows you to specify the path of the file that contain the data to send.
Here's the documentation:
--post-data=string
--post-file=file
Use POST as the method for all HTTP requests and send the specified data
in the request body. --post-data sends string as data, whereas
--post-file sends the contents of file. Other than that, they work in
exactly the same way. In particular, they both expect content of the
form "key1=value1&key2=value2", with percent-encoding for special
characters; the only difference is that one expects its content as a
command-line parameter and the other accepts its content from a file. In
particular, --post-file is not for transmitting files as form
attachments: those must appear as "key=value" data (with appropriate
percent-coding) just like everything else. Wget does not currently
support "multipart/form-data" for transmitting POST data; only
"application/x-www-form-urlencoded". Only one of --post-data and
--post-file should be specified.
Regarding your authentication token, it should either be provided in the header, in the path of the url, or in the data itself. This must be indicated somewhere in the documentation of the service you use. In a POST request, as in a GET request, you must specify the data using keys and values. This way the server will be able to receive multiple information with specific names. It's similar with variables.
Hence, you can't just send a magic token to the server, you also need to specify the name of the key. If the key is "token", then it should be token=YOUR_TOKEN
.
wget --post-data 'user=foo&password=bar' http://example.com/auth.php
Also, you should consider using curl if you can because it is easier to send files using it. There are many examples on the Internet for that.
How to make http/https POST requests manually?
You can use wget
for this. From the manual
it supports digest authentication and can send POST requests.
There seems to be a GUI at wget::gui, but I don't know how reliable or complete it is.
How to send a GET request with Wget and output it on the screen
Instead of -O response_from_API_server.json
try -O-
. Instead of saving the response to a file it prints to stdout.
Send urlencoded POST request with wget
as suggested in the comments by @Roadowl, bash itself can urlencode data, the wget invocation could look like
wget --post-data=$(printf "url="; urlencode $u) http://example.com
using urlencode implementation from https://gist.github.com/cdown/1163649 ,
urlencode() {
# urlencode <string>
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
.. probably won't work will null bytes though, given that bash doesn't like null bytes and probably won't allow them in variables
Sending multiline command output via wget as post-data
You do not escape - you quote the usage. Check your scripts with shellcheck.
... --post-data="$(ls -lah)" ...
BASH - post array with wget --post-data
That wget command looks OK, and worked in a small test I did.
Your problem is probably on the server side.
Related Topics
How Would One Disable Nagle's Algorithm in Linux
Error on Execution -Version 'Qt_5' Not Found Required By
How Does Bash Deal with Nested Quotes
How to Trim White Space from a Variable in Awk
Memory Limit to a 32-Bit Process Running on a 64-Bit Linux Os
Limiting Memory Usage in R Under Linux
Number of Executed Instructions Different for Hello World Program Nasm Assembly and C
How to Export the Variable Through Script File
How to Copy the Output of a Command Directly into My Clipboard
How to Search Contents of Multiple PDF Files
What Is the Linux Equivalent to Dos Pause
How to Create a Link to a Directory
Find All Files with Name Containing String
How to Find Directory of Some Command
Parallel Download Using Curl Command Line Utility
Term Environment Variable Not Set