Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnk's avatar
Level 1

Make a post request to an API error (Passing in the "body" request option as an array to send a POST request has been deprecated. )

I want to make a post request to an API but its appearing an error:

Passing in the "body" request option as an array 
    to send a POST request has been deprecated. 
    Please use the "form_params" request option to 
    send a application/x-www-form-urlencoded request,
    or the "multipart" request option to send a multipart/form-data request.

The code:

      public function createClient()
        {
    
            $client = new \GuzzleHttp\Client();
            
            $array = ['invoice' => [
                'date' => date('Y-m-d H:i:s'),
                'due_date' => date('Y-m-d H:i:s'),
                'client' => ['name' => 'John', 'code' => ''],
                'items' => [
                    'item' => [
                        'name' => 'item1',
                        'description' => 'item1 desc',
                        'unit_price' => '10',
                        'quantity' => '1.0'
                    ],
                    'item' => [
                        'name' => 'item2',
                        'description' => 'item 2 desc',
                        'unit_price' => '10',
                        'quantity' => '1.0'
                    ]
                ]
            ]];
    
            $result = ArrayToXml::convert($array);
    
            $response = $client->request('POST', 'https://testname.app.invoicexpress.com/invoices.xml', [
                'query' => ['api_key' => '...'], 'body' => [ $result],
            ]);
            dd($response->getStatusCode());
   
        }
0 likes
12 replies
bobbybouwmann's avatar
Level 88

Instead of body you need to use form_params as the error message suggests!

This is because you use a newer version of Guzzle!

2 likes
johnk's avatar
Level 1

Thanks, now appears:

Client error: `POST https://testnae.app.invoicexpress.com/invoices.xml?api_key=...` resulted in a `422 Unprocessable Entity` response: <?xml version="1.0" encoding="UTF-8"?> <errors> <error>Expected &lt;invoice&gt;</error> </errors>

Do you know what can be the error?

bobbybouwmann's avatar

It says expected "invoice"! So you probably miss that key in your request. When you get a 422 http status code, it probably means that the data you submitted is not valid. So either a required field is missing or one of the values is invalid

bobbybouwmann's avatar

Also note that you need to post XML instead of an array. Right now it will be converted to json before it will be posted to the API. Instead you need to rebuild your array to xml and post that!

Array to XML: https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml

Guzzle post XML: https://stackoverflow.com/questions/34726530/proper-way-to-send-post-xml-with-guzzle-6

I have the feeling that you've never done this before and that we're doing your homework! Try to play with some API's first and see how they work ;)

johnk's avatar
Level 1

I already have: "$result = ArrayToXml::convert($array); " and $result shows the XML and it seems correct and it has the :

<?xml version="1.0"?>\n
<root>
    <invoice>
        <date>2018-06-17 18:58:51</date>
        <due_date>2018-06-17 18:58:51</due_date>
        <client>
                <name>John</name>
                <code></code>
        </client>
        <items>
            <item>
                <name>reg1</name>
                <description>reg1 on evt</description>
                <unit_price>10</unit_price>
                <quantity>1.0</quantity>
            </item>
        </items>
    </invoice>
</root>
bobbybouwmann's avatar

@johnk It's incorrect, because it starts with <root>! It should start with <invoice>!

1 like
johnk's avatar
Level 1

Thanks, but in the $array there is no root do you know why it appears in the $result?

bobbybouwmann's avatar

That's default behaviour of converting an array to xml! If you don't want that you need to provide a string of XML to the post request! Check the links in my previous messages ;)

1 like
johnk's avatar
Level 1

Thanks, it seems that they have a JSON version of the api also "https://developers.invoicexpress.com/docs/versions/2.0.0/resources/invoices".

To create a new client along with the invoice the curl command is like:

curl --request POST \
  --url 'https://account_name.app.invoicexpress.com/:document-type.json?api_key=YOUR%20API%20KEY%20HERE' \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --data '{"invoice":{"date":"03/12/2017","due_date":"03/12/2017","client":{"name":"Client Name","code":"A1"},"items":[{"name":"Item Name","description":"Item Description","unit_price":"100","quantity":"5"}]}}'

But using guzzle like below it shows:

Client error: `POST https://testname.app.invoicexpress.com/:document-type.json?api_key=...` resulted in a `404 Not Found` response: <!doctype html> <!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]--> <!--[if IE 7]> <html class="n (truncated...)

Do you know what is the error in the createInvoice()?

createClient():

public function createInvoice()
    {

        $client = new \GuzzleHttp\Client();

        $array = ['invoice' => [
            'date' => date('Y-m-d H:i:s'),
            'due_date' => date('Y-m-d H:i:s'),
            'client' => ['name' => 'John', 'code' => ''],
            'items' => [
                'item' => [
                    'name' => 'item1',
                    'description' => 'item1 desc',
                    'unit_price' => '10',
                    'quantity' => '1.0'
                ],
                'item' => [
                    'name' => 'item2',
                    'description' => 'item 2 desc',
                    'unit_price' => '10',
                    'quantity' => '1.0'
                ]
            ]
        ]];

        $response = $client->request('POST', 'https://testname.app.invoicexpress.com/:document-type.json', [
            'query' => ['api_key' => '...'], 'form_params' => [ json_encode($array)],
        ]);
        dd($response->getStatusCode());

    }


bobbybouwmann's avatar

So when it's a json api you don't have to json_encode it anymore! Also you need to post to a valid url. So replace /: with /

1 like
johnk's avatar
Level 1

Thanks, but it shows the same error with:

$response = $client->request('POST', 'https://testname.app.invoicexpress.com/document-type.json', [
            'query' => ['api_key' => '...'], 'form_params' => [$array],
        ]);

Error:



Client error: `POST https://testname.app.invoicexpress.com/document-type.json?api_key=...` resulted in a `404 Not Found` response: <!doctype html> <!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]--> <!--[if IE 7]> <html class="n (truncated...)

The name 'testname' and apki key are correct, acessing this url it works "https://testname.app.invoicexpress.com/invoices.json?api_key=...". It shows:

invoices    []

bobbybouwmann's avatar

It says 404 not found, so the URL must be incorrect or the data you try to fetch does not exists!

Please or to participate in this conversation.