dongzhi5386 2019-07-30 21:34
浏览 460
已采纳

使用Guzzle将数据和文件一起发布到Laravel API

I've two working project communicating between each other via an API built in a Laravel. So far there is only simple POST requests made with GuzzleHttp 6.

And I am currently trying to have a new POST request made from 1 to 2, which would send a couple of simple fields along with one file. Project 1 has a form, on the form submit I handle the data and want to send them to project 2 via a POST request to this new API endpoint.

I've tried different guzzle options 'multipart', 'form_data' etc and realised they may not be combined together. Now I understood that this options are exclusive and using only "multipart" seems the way to go. But when I send my request to Laravel no data nor file are there.

Here is the code for my request

$options = ‌[
  'multipart' => 
  [
    [
      'name' => 'data',
      'contents' => '{"field_1":"Test","field_2":"Test","field_3":"Test"}',
      'headers' => 
        [
            'Content-Type' => 'application/json',
        ],
    ],
    [
      'name' => 'file',
      'filename' => 'test.pdf',
      'Mime-Type' => 'application/pdf',
      'contents' => file_get_contents($_FILEs['text_file']['temp_name']),
    ]
  ]
];

$this->client->request('POST', "api/test_post", $options)

I also gve this a try:

$options = ‌[
  'multipart' => 
  [
    [
      'name' => 'field_1',
      'contents' => 'Test',
    ],
    [
      'name' => 'field_2',
      'contents' => 'Test',
    ],
    [
      'name' => 'file',
      'filename' => 'test.pdf',
      'Mime-Type' => 'application/pdf',
      'contents' => fopen($_FILEs['text_file']['temp_name'],'r'),
    ]
  ]
];

$this->client->request('POST', "api/test_post", $options)

If I look the request content on the receiving end, nothing is there. No field or file.

I've seen couples posts, some say to include headers some say not too. I kinda got lost and amd now running out of ideas.

I would expecet the infos to be as if they where form post I guess:

  • $request->inpust('field_1') -> 'test'
  • $request->inpust('field_2') -> 'test'
  • $request->inpust('field_3') -> 'test'
  • $request->file('file') -> my uploaded file

Also I should point out that I am not exactly sure how multipart/form-data works, so that might not help me.

If you can point me to the right direction, that would help a lot

  • 写回答

1条回答 默认 最新

  • douhuibo5635 2019-07-31 21:09
    关注

    Well I finally figured it out. The second example from above is the way to go also be sure to check the headers of the request and the client...

    As this API has been running for quite some time and was only doing json type requests, the Client was instantiated with

    $options = [
      headers => [ 'Content-Type' => 'application/json']
    ]
    

    Which, as stated in multiple answers across the internet, prevents Guzzle to automatically set the Content-Type depending of the request options.

    In my case, removing this line made Guzzle enable to set it properly when provided with 'multipart' option.

    Also, as all other requests are using the 'json' options, Guzzle also works it's magic and set 'Content-Type' => 'application/json' as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划