84

我想使用 wget 将图片上传到远程服务器,使用身份验证令牌“AUTH_1624582364932749DFHDD”到“测试”文件夹。

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

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

有什么建议么?

4

1 回答 1

114

Wget 当前不支持“multipart/form-data”数据。--post-file不用于将文件作为表单附件传输,它需要格式为: 的数据key=value&otherkey=example。如果您发送相应的标头,实际上可以发布其他格式(json)。

--post-data并且--post-file工作方式相同:唯一的区别是--post-data允许您在命令行中指定数据,同时--post-file允许您指定包含要发送的数据的文件的路径。

这是文档:

 --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.

于 2013-07-17T12:36:29.583 回答