Linux 使用 Wget 发布请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17699666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:17:22  来源:igfitidea点击:

Post request with Wget?

linuxpostwget

提问by Dady

I want to use wget to upload a picture to a distant server, using an authentication token, 'AUTH_1624582364932749DFHDD', to the 'test' folder.

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

This command doesn't work (authorization failed), and I want to make sure that it's not about syntax:

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

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

Any suggestions?

有什么建议?

采纳答案by Maxime Chéramy

Wget currently only supports x-www-form-urlencoded data. --post-fileis not for transmitting files as form attachments, it expects data with the form: key=value&otherkey=example.

Wget 目前只支持 x-www-form-urlencoded 数据。--post-file不用于将文件作为表单附件传输,它需要以下形式的数据:key=value&otherkey=example

--post-dataand --post-filework the same way: the only difference is that --post-dataallows you to specify the data in the command line, while --post-fileallows you to specify the path of the file that contain the data to send.

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

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.

关于您的身份验证令牌,它应该在标头、url 路径或数据本身中提供。这必须在您使用的服务的文档中的某处指明。在 POST 请求中,就像在 GET 请求中一样,您必须使用键和值指定数据。这样服务器将能够接收具有特定名称的多个信息。这与变量类似。

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.

因此,您不能只向服务器发送魔术令牌,还需要指定密钥的名称。如果密钥是“令牌”,那么它应该是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.

此外,如果可以,您应该考虑使用 curl,因为使用它发送文件更容易。互联网上有很多这样的例子。