0

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

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

}

Wihout [] and only $array in the 'form_params' shows:

 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"}]}
  • 1
    Possible duplicate of [How can I use Guzzle to send a POST request in JSON?](https://stackoverflow.com/questions/22244738/how-can-i-use-guzzle-to-send-a-post-request-in-json) – Script47 Jun 18 '18 at 13:38

2 Answers2

0

Try renaming your param 'form_param' with 'json'

Like : 'form_params' => [$array] to 'json' => [$array]

Pradeep Kumar Kushwaha
  • 2,231
  • 3
  • 23
  • 34
  • Thanks, but also appear "Client error: `POST https://testname.app.invoicexpress.com/invoices.json?api_key=...` resulted in a `422 Unprocessable Entity` response: {"errors":[{"error":"Expected "}]} ". –  Jun 18 '18 at 13:47
0

Hi for someone with the same problem please use this class for send arrays through guzzle

RequestOptions::JSON => your_array   

Use GET method too, here a example :

$response = $client->request('GET', $url, [
            'http_errors'        => true,
            RequestOptions::JSON => your_array,
        ]);

if this doesn't work remember than the request url must have ssl