如何为多部分POST请求设置自定义边界?以下请求选项配置不起作用。
'headers' => ['Content-Type' => 'multipart/form-data; boundary=CUSTOM_BOUNDARY']
答案 0 :(得分:3)
Guzzle使用psr7将多部分表单字段组合到请求正文中。处理自定义边界的最正确方法是使用 GuzzleHttp \ Psr7 \ MultipartStream 。
$boundary = 'my_custom_boundary';
$multipart_form = [
[
'name' => 'upload_id',
'contents' => $upload_id,
],
[
'name' => '_uuid',
'contents' => $uuid,
],
...
];
$params = [
'headers' => [
'Connection' => 'close',
'Content-Type' => 'multipart/form-data; boundary='.$boundary,
],
'body' => new GuzzleHttp\Psr7\MultipartStream($multipart_form, $boundary), // here is all the magic
];
$res = $this->client->request($method, $url, $params);
答案 1 :(得分:1)
我有同样的错误,这就是我如何解决它。
//encode field
$field_string = json_encode($field_data);
//read file
$file_string = Flysystem::read($config['doc_path']);
// hack, request body, inject field and file into requet body, set boundary
$request_body =
"\r\n"
."\r\n"
."--customboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$field_string\r\n"
."--customboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=".$config['deal_name'].";documentid=".$config['deal_id']." \r\n"
."\r\n"
."$file_string\r\n"
."--customboundary--\r\n"
."\r\n";
//create request, boundary is required for docusign api
$result = $this->client->createRequest('POST',"$this->baseUrl/templates", [
'headers' => [
'Content-Type' => 'multipart/form-data;boundary=customboundary',
'Content-Length' => strlen($request_body),
'X-DocuSign-Authentication' => json_encode([
'Username' => Config::get('docusign.email'),
'Password' => Config::get('docusign.password'),
'IntegratorKey' => Config::get('docusign.integratorKey')
]),
],
'body' => $request_body
]);