【问题标题】:(PHP) Ixudra/Curl cannot post file(PHP) Ixudra/Curl 无法发布文件
【发布时间】:2018-10-29 11:54:30
【问题描述】:

我在我的 laravel 项目中使用 Ixudra/Curl。 当我尝试将文件上传到 Java API 时遇到问题。 使用 Curl 使用命令行正在使用以下文件:

curl -X POST \
http://<<api url>>' \
-H 'Content-Type: multipart/form-data' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=xxxxx\
-F 'data={"tables" : ... (no need to post all data)' \
-F 'file=@C:\Users\UserName\Downloads\Test.xls'

我尝试使用 PHP 代码但没有成功:

Curl::to('<<api url>>')
     ->withContentType('multipart/form-data; boundary=' . hash('sha256', uniqid('', true)))
     ->withData(array('data' => {"tables" : ...  ))
     ->withFile('file', 'C:\Users\UserName\Downloads\Test.xls', 'application/vnd.ms-excel', 'Test.xls')
     ->post();

我用这段代码得到的响应是:

"exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"所需的请求部分'文件'不存在"

有什么想法吗?感谢您的宝贵时间

【问题讨论】:

  • 我没有使用过这个包,但你可以尝试使用guzzle/http。我认为它应该可以解决您的问题

标签: php laravel rest curl


【解决方案1】:

问题解决了, 似乎这个库不喜欢我指定内容类型。 删除该行后,一切都按预期进行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2018-02-12
    相关资源
    最近更新 更多