向 Wget 发送请求?

我想使用 wget 将图片上传到一个远程服务器,使用一个身份验证令牌“ AUTH _ 1624582364932749DFHDD”到“ test”文件夹。

这个命令不起作用(授权失败) ,我想确保它与语法无关:

wget --post-file=nature.jpg http://ipadress:8080/v1/AUTH_test/test/ --post-data="AUTH_1624582364932749DFHDD"

有什么建议吗?

319439 次浏览

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.