5

I develop website using Laravel 5.5 and Guzzle 6.3.

I encountered a problem when trying to guzzle post nested arrays when trying to do create folder to BOX using API.

$url = $this->api_url . "/folders";
$headers = [
    'Authorization' => 'Bearer ' . $this->access_token,        
];
$client = new Client();
$response = $client->post($url, [
    'headers' => $headers, 
    'form_params' => [
        'name' => $name,
        'parent' => [
            'id' => $parent_id
        ]
    ]
]);

It shows me errors like this:

Entity body should be a correctly nested resource attribute name/value pair

I also already tried using shell_exec curl so it run curl from command prompt and it gives me same error like this

picture

But when I tried to run from cygwin the curl works fine.

I also can do upload using multipart request nested array works fine.

I don't know why I'm getting with this nested array issue when the nested array works fine with multipart request.

Reference for box documentation POST is here.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145

2 Answers2

2

According to the docs you can't use the multipart option:

form_params cannot be used with the multipart option. You will need to use one or the other. Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests.

This option cannot be used with body, multipart, or json

So maybe try to set the header when creating the Client instance:

$url = $this->api_url . "/folders";

$client = new Client([
    'headers' => [
        'Authorization' => 'Bearer ' . $this->access_token,
        'Accept'        => 'application/json',        
    ]
]);

$response = $client->post($url, [ 
    'json' => [
        'name' => $name,
        'parent' => [
            'id' => $parent_id
        ]
    ]
]);

actually after reading box reference again, post request without files upload it accepts application/json , which is form_params used for application/x-www-form-urlencoded

Community
  • 1
  • 1
Daniel
  • 971
  • 9
  • 23
  • 1
    yes i do understand form_params cannot be used with the multipart option, i already tried what you mentioned, its still gave me the same error https://developer.box.com/v2.0/reference#create-a-new-folder i knew it doesnt need multipart I only mention , my code/my curl for uploading it also has nested array and it works perfectly fine – Newbie User Sep 19 '18 at 18:14
  • okay i figure it out with your advice , i will edit your suggestion – Newbie User Sep 19 '18 at 18:22
  • then maybe the problem is that you are passing an associative array for the parent. [this answer](https://stackoverflow.com/a/30753498/8374833) might help you – Daniel Sep 19 '18 at 18:23
  • yes your first suggestion actually half correct, move out the headers to invoke guzzle and then add application json and change form_params to json thanks for help Daniel ! – Newbie User Sep 19 '18 at 18:29
1

For you to make any http request of data containing nested fields; you must include Content-Type on the headers; then set it to application/x-www-form-urlencoded like so:

$url = $this->api_url . "/folders";
$headers = [
    'Accept'       => 'application/json',
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Authorization' => 'Bearer ' . $this->access_token,
];
$client = new Client();
$response = $client->post($url, [
    'headers' => $headers,
    'form_params' => [
        'name' => $name,
        'parent' => [
            'id' => $parent_id
        ]
    ]
]);
halfer
  • 19,824
  • 17
  • 99
  • 186
Emeka Augustine
  • 891
  • 1
  • 12
  • 17