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

I want to make a post request to an API to create a new client but its appearing an 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...)

In the documentation "https://developers.invoicexpress.com/docs/versions/2.0.0/resources/invoices" says that 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 with the code below instead of the curl command, it shows that error.

    public function generateInvoice()
    {
    
        $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/invoices.json', [
            'query' => ['api_key' => '...'], 'form_params' => [$array],
        ]);
        dd($response->getStatusCode());
    
    }

Acessing the url directly "testname.app.invoicexpress.com/invoices.json?api_key=..."; the error dont appear, it appears "{ "invoices": [], "pagination": { "per_page": 10, "current_page": 1, "total_entries": 0, "total_pages": 0 } }".

0 likes
9 replies
johnk's avatar
Level 1

Acessing the url directly "testname.app.invoicexpress.com/invoices.json?api_key=..."; the error dont appear, it appears "{ "invoices": [], "pagination": { "per_page": 10, "current_page": 1, "total_entries": 0, "total_pages": 0 } }".

johnk's avatar
Level 1

Thanks, without [] appears:

 "Client error: POST https://testname.app.invoicexpress.com/invoices.json?api_key=... resulted in a 422 Unprocessable Entity response: {"errors":[{"error":"Items element should be of type array"}]}"

And with document-type.json also appears:


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...)

grenadecx's avatar

Actually, this one is now correct:

 "Client error: POST https://testname.app.invoicexpress.com/invoices.json?api_key=... resulted in a 422 Unprocessable Entity response: {"errors":[{"error":"Items element should be of type array"}]}"

Now you need to fix the array

'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'
    ]
]

into

'items' => [
    [
        'name' => 'item1',
        'description' => 'item1 desc',
        'unit_price' => '10',
        'quantity' => '1.0'
    ],
    [
        'name' => 'item2',
        'description' => 'item 2 desc',
        'unit_price' => '10',
        'quantity' => '1.0'
    ]
]
1 like
johnk's avatar
Level 1

Thanks, but with the code below still appears:

Client error: `POST https://testname.app.invoicexpress.com/invoices.json?api_key=...` resulted in a `422 Unprocessable Entity` response: {"errors":[{"error":"Items element should be of type array"}]}

createClient():


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' => [
            [
                'name' => 'item1',
                'description' => 'item1 desc',
                'unit_price' => '10',
                'quantity' => '1.0'
            ],
            [
                'name' => 'item2',
                'description' => 'item 2 desc',
                'unit_price' => '10',
                'quantity' => '1.0'
            ]
        ]

    ]];

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

}
grenadecx's avatar

Not sure then. I've replicated the exact array you used in curl where you claimed it to be working:

    $array = [
        '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'
                ]
            ]
        ]
    ];

I might be wrong, maybe you need the array around the $array

'form_params' => [$array],
1 like
johnk's avatar
Level 1

Thanks with [$array] the error changes to:

Client error: `POST https://testname.app.invoicexpress.com/invoices.json?api_key=...` resulted in a `422 Unprocessable Entity` response: {"errors":[{"error":"Expected <invoice>"}]}
grenadecx's avatar
Level 7

I'm not sure what's missing or what makes it invalid. Maybe it's picky on how the formdata is being sent. Try replacing form_params with json and see if that changes anything. Maybe someone else will chip in and help out.

1 like

Please or to participate in this conversation.