dts777777 2015-11-14 22:00
浏览 14
已采纳

如何使用laravel向box.com API发送帖子请求?

I'm trying to make a post request from my controller to the Box.com API. The request will upload a file to my Box.com account. The following code is on my view:

<form action="/accept" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}

    <input type="file" name="filename">
    <input type="text" name="parent_id" value="0">

    <button type="submit">Submit</button>
</form>

In my controller, I'm not getting any clue of how can I send the post request to the box.com API which will send the form data with an authorization header. The Box.com API require the authorization header to accept the request.

However, in Laravel documentation they've a section where I found the following code:

return response($content)
        ->header('Content-Type', application/json)
        ->header('Authorization', 'Bearer XXXXXXXXXXXXXXXX');

I can set the authorization header using this however, how the form data would be send then?

Here's the Box.com API URL to accept the post request:

https://upload.box.com/api/2.0/files/content
  • 写回答

2条回答 默认 最新

  • dongtui9168 2015-11-15 04:38
    关注

    I definitely agree with pespantelis, using a PHP HTTP Client like Guzzle is a great way to go with what you wish to do.

    Once you're all set up it is just a matter of formatting your request, e.g you could do what you're trying to do like so:

    Route::get( '/', function ()
    {
        $client = new GuzzleHttp\Client( ["base_uri" => "https://upload.box.com/api/2.0/"] );
    
        $response = $client->request( "GET", "files/content", ["headers" => ["Authorization" => "Bearer XXXXXXXXXXXXXXXX", "Content-Type" => "application/json"]] );
    
        return Response::json($response);
    } );
    

    Although I would consider using the Official SDK or the Community-Built SDK as pespantelis suggests as that may be easier to grasp since they lay it all out for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题