【问题标题】:Post request with Wget?使用 Wget 发布请求?
【发布时间】:2013-07-15 23:58:24
【问题描述】:

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

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

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

有什么建议吗?

【问题讨论】:

    标签: linux post wget


    【解决方案1】:

    Wget 当前不支持“multipart/form-data”数据。 --post-file 不用于将文件作为表单附件传输,它需要格式为:key=value&otherkey=example 的数据。如果发送相应的header,其实也可以发布其他格式(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.
    

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

    因此,您不能只向服务器发送一个魔法令牌,您还需要指定密钥的名称。如果密钥是“令牌”,那么它应该是token=YOUR_TOKEN

    wget --post-data 'user=foo&password=bar' http://example.com/auth.php
    

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

    【讨论】:

    • 在这种情况下如何发送我的身份验证令牌?
    • 如果它是一个基本的 html 表单,你将如何命名包含令牌的输入?因为那应该是:token=AUTH_1624582364932749DFHDD
    • 我想说@Hasturkun :D
    • 不,它不是 HTML 表单,我想在服务器上上传图片“nature.jpg”,该服务器需要令牌“AUTH_*******”来识别用户,所以我必须使用 POST 请求发送文件和令牌(字符串)
    • 终于成功了: curl -v -H 'X-Auth-Token: AUTH_tk8c1eecae98e845159cebf69f06bb10f2' ipadress:8080/v1/AUTH_test/test -T nature.jpg
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2013-02-22
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多