Laravel HTTP 客户端 - 如何发送文件 [已关闭]

问题描述 投票:0回答:1

我有一个控制器功能,例如

public function storeBlog(Request $request)
{
  // Here i am receiving file like $request->file('image');
}

现在我想将该文件发送到 API 端点,例如

Http::post('http://example.com/v1/blog/store', $request->all());

我收到了所有请求,但没有收到文件,我知道我们需要将

POST
数据作为
multipart
传递。我怎样才能做到这一点?

laravel api laravel-api
1个回答
25
投票

您应该使用 Http::attach 上传文件。

public function storeBlog(Request $request)
{
    // check file is present and has no problem uploading it
    if ($request->hasFile('image') && $request->file('photo')->isValid()) {
        // get Illuminate\Http\UploadedFile instance
        $image = $request->file('image');

        // post request with attachment
        Http::attach('attachment', file_get_contents($image), 'image.jpg')
            ->post('example.com/v1/blog/store', $request->all());
    } else {
        Http::post('http://example.com/v1/blog/store', $request->all());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.