Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Client Post - Multipart with multidimensional array body #43710

Closed
codoffer opened this issue Aug 15, 2022 · 4 comments
Closed

HTTP Client Post - Multipart with multidimensional array body #43710

codoffer opened this issue Aug 15, 2022 · 4 comments

Comments

@codoffer
Copy link

  • Laravel Version: 9.27,
  • PHP Version: 8.1.7
  • Database Driver & Version: MySQL

Description:

I am getting errors with attaching a file and body with the multi-dimensional array. I want to send multi-dimensional array with attachments files (with attachment field).

Same issue was opened before but closed without solution.

#37174

Steps To Reproduce:

NOT WORKING

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\File;

$path = storage_path('file.txt');

Http::acceptJson()
    ->attach('attachments[]', File::get($path), File::basename($path))
    ->post($destination, [
        'name' => 'John Smith',
        'providers' => [1,2,3]
]);

with throw InvalidArgumentException with message 'A 'contents' key is required'
whereas will be just fine

WORKING

Http::acceptJson()
    ->attach('attachments[]', File::get($path), File::basename($path))
    ->post($destination, [
        'name' => 'John Smith',
        'providers' => '1'
]);
@driesvints
Copy link
Member

You're trying to post an array (providers) in a multipart HTTP request. That doesn't work. You're not sending JSON here but file parts.

@codoffer
Copy link
Author

codoffer commented Aug 16, 2022

I have tried with/without JSON also. But it's not accepting.

@driesvints
Copy link
Member

You can't send files over JSON. You need multipart for that.

@codoffer
Copy link
Author

I want to send

{
"reply": {
"body": "string",
},
"attachments": [
"string"
]
}

attachments must upload the entire payload as multipart/form-data content-type and want to specify each parameter as form-data together with attachments[] as files.

I am not sure, How to handle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants