0

I want to make a post request to this API "https://invoicexpress.com/api/invoices/create" to create a new client.

In that documentatio page says that the required fields are: date, due_date, client and items.

I have the code below but Im getting an error:

Client error: POST https://nametest.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 why that error is appaering?

Code:

public function createClient(){

        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', 'https://nametest.app.invoicexpress.com/invoices.xml', [
            'query' => ['api_key' => '...'],
            'date' => date('Y-m-d H:i:s'),
            'due_date' => date('Y-m-d H:i:s'),
            'client'   => 'John',
            'code'  => '',

            'items' => [
                'item' => [
                    'name' => 'item1',
                    'description' => 'item1 desc',
                    'unit_price' => '10',
                    'quantity' => '1'
                ],

                'item' => [
                    'name' => 'item2',
                    'description' => 'item2 des',
                    'unit_price' => '10',
                    'quantity' => '1'
                ]
            ]

        ]);
        dd($response->getStatusCode());

    }
  • You are not sending a body, and the body should be in xml format. https://stackoverflow.com/questions/34726530/proper-way-to-send-post-xml-with-guzzle-6 – user2094178 Jun 17 '18 at 16:12

1 Answers1

0

The documentation indicates that the top-level element in your XML must be <invoice>, that's missing from your data. It should look more like:

$response = $client->request('POST', 'https://nametest.app.invoicexpress.com/invoices.xml', [
    'invoice' => [
        'query' => ['api_key' => '...'],
        ...
    ]
]);

Also noticed that you've got 'client' => 'John',, but the docs indicate that it should be more like 'client' => ['name' => 'John'],.

Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35
  • Thanks so the "code" parameter should also be inside the 'client' array? –  Jun 17 '18 at 16:08
  • With: public function createClient(){ $client = new \GuzzleHttp\Client(); $response = $client->request('POST', 'https://testname.invoicexpress.com/invoices.xml', [ 'query' => ['api_key' => '....'], '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' ] ] ] ]); dd($response->getStatusCode()); }. –  Jun 17 '18 at 16:11