PHP Guzzle. How to set custom boundary to the multipart POST request?

前端 未结 2 1309
傲寒
傲寒 2021-01-24 18:40

How can I set custom boundary to the multipart POST request? Following request options configuration doesn\'t work.

\'headers\' => [\'Content-Type\' => \'m         


        
2条回答
  •  粉色の甜心
    2021-01-24 19:13

    I'm having the same error, here's how i solve it.

    //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
    ]);
    

提交回复
热议问题