代码之家  ›  专栏  ›  技术社区  ›  Upperstage

使用curl上载文件内容

  •  0
  • Upperstage  · 技术社区  · 14 年前

    我有一个使用curl将数据发布到服务器的脚本。当我使用HTML表单发布相同的数据时,发布看起来像这样,一切都很好:

    description=Something&name=aName&xml=wholeBiunchOfData&xslt=moreData
    

    XML和XSLT很大,而且会发生变化;我更喜欢在外部文件中维护它们。但是,以下内容并不像我期望的那样有效;

    curl --cookie cjar --cookie-jar cjar --location --output NUL ^
     --data "name=aName&description=Something" ^
        --data "xml=@localFile.xml" ^
     --data "xslt=@localFile.xslt" ^
     http://someUrl.html
    

    我尝试了@和本地文件的各种组合,但没有成功。如何发布文件的内容?

    2 回复  |  直到 12 年前
        1
  •  1
  •   mlschechter    14 年前

    我建议尝试以下方法:

    curl --cookie cjar --cookie-jar cjar --location --output NUL ^
     --data "name=aName&description=Something" ^
        --data-urlencode "xml@localFile.xml" ^
     --data-urlencode "xslt@localFile.xslt" ^
     http://someUrl.html
    

    XML(包括样式表)在成为URL的一部分之前需要进行URL编码。

    您也可以使用 --trace-ascii - 作为将输入和输出转储到标准输出以进行进一步调试的附加参数,您可以在主服务器上找到更多信息 man page .

    希望这有帮助!

        2
  •  2
  •   stimms    12 年前

    查看手册页,它看起来--data@file语法不允许使用变量名,它必须在文件中。 http://paulstimesink.com/2005/06/29/http-post-with-curl/ . 你也可以尝试使用倒勾

    curl --cookie cjar --cookie-jar cjar --location --output NUL ^
     --data "name=aName&description=Something" ^
     --data "xml=`cat localFile.xml`" ^
     --data "xslt=`cat someFile.xml`" ^
     http://someUrl.html