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 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。