Upload file using Guzzle 6 endpoint in API

I can upload the file to the API endpoint using Postman.

I am trying to translate this to uploading a file from a form, uploading it using Laravel and sending it to the endpoint using Guzzle 6.

Screenshot of how it looks in Postman (I specifically missed the POST URL) enter image description here

Below is the text that it generates when you click the Create Code link in POSTMAN:

POST /api/file-submissions HTTP/1.1 Host: strippedhostname.com Authorization: Basic 340r9iu34ontoeioir Cache-Control: no-cache Postman-Token: 6e0c3123-c07c-ce54-8ba1-0a1a402b53f1 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW ----WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="FileContents"; filename="" Content-Type: ----WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="FileInfo" { "name": "_aaaa.txt", "clientNumber": "102425", "type": "Writeoff" } ----WebKitFormBoundary7MA4YWxkTrZu0gW 

Below is the controller function to save the file and other information. The file is loading correctly, I can get information about the file.

I think the problem is with setting up the multipart and headers array with the correct data.

 public function fileUploadPost(Request $request) { $data_posted = $request->input(); $endpoint = "/file-submissions"; $response = array(); $file = $request->file('filename'); $name = time() . '_' . $file->getClientOriginalName(); $path = base_path() .'/public_html/documents/'; $resource = fopen($file,"r") or die("File upload Problems"); $file->move($path, $name); // { "name": "test_upload.txt", "clientNumber": "102425", "type": "Writeoff" } $fileinfo = array( 'name' => $name, 'clientNumber' => "102425", 'type' => 'Writeoff', ); $client = new \GuzzleHttp\Client(); $res = $client->request('POST', $this->base_api . $endpoint, [ 'auth' => [env('API_USERNAME'), env('API_PASSWORD')], 'multipart' => [ [ 'name' => $name, 'FileContents' => fopen($path . $name, 'r'), 'contents' => fopen($path . $name, 'r'), 'FileInfo' => json_encode($fileinfo), 'headers' => [ 'Content-Type' => 'text/plain', 'Content-Disposition' => 'form-data; name="FileContents"; filename="'. $name .'"', ], // 'contents' => $resource, ] ], ]); if($res->getStatusCode() != 200) exit("Something happened, could not retrieve data"); $response = json_decode($res->getBody()); var_dump($response); exit(); } 

The error I get is a screenshot of how it is displayed using the Laravel debug mode:

enter image description here

+7
api php laravel guzzle guzzle6
source share
2 answers

The way you send POST data is incorrect, so the received data is incorrect.

Guzzle docs :

The multipart value is an array of associative arrays, each containing the following pairs of key values:

name : (string, required) the name of the form field

contents :( StreamInterface / resource / string, required) Data to use in the form element.

headers : (array) An optional associative array of custom headers to use with the form element.

filename : (string) Optional string to send as the file name in part.

Using the keys from the list above and setting unnecessary headers without dividing each field into one array will result in an incorrect request.

 $res = $client->request('POST', $this->base_api . $endpoint, [ 'auth' => [ env('API_USERNAME'), env('API_PASSWORD') ], 'multipart' => [ [ 'name' => 'FileContents', 'contents' => file_get_contents($path . $name), 'filename' => $name ], [ 'name' => 'FileInfo', 'contents' => json_encode($fileinfo) ] ], ]); 
+16
source share
 $body = fopen('/path/to/file', 'r'); $r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]); 

http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=file

0
source share

All Articles