php Laravel post file with HTTP::attach A 'contents' key is required错误

xoefb8l8  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(67)

我在Laravel post文件上遇到错误,HTTP::attach错误:需要'contents'键

static public function apiRequestWithFile($method, $endpoint, array $params = [], $file = null)
    {
        $apiKey = env('API_KEY');
        $baseUrl = env('BASE_URL');

        $file = fopen($file, 'r');

        $response = Http::withHeaders([
                'Authorization' => $apiKey,
            ])->attach('file', $file, 'file.csv')
            ->$method($baseUrl . $endpoint, $params);

        return $response->json();
    }

字符串
需要获取“内容”密钥

遵循laravel文档


的数据

qoefvg9y

qoefvg9y1#

因为使用attach方法,你的enctype会变成“Multipart/Form-data”,所以你需要像这样传递你的param数组:

$params = [
                [
                    'name' => 'param 1',
                    'contents' => 'param 1 contents'
                ],
                [
                    'name' => 'param 2',
                    'contents' => 'param 2 contents'
                ]
];

字符串

相关问题