Delete Invoice from API returns 416 Response Code

Hey There,

Using the latest PHP SDK version (25.1.0.20230119), I’m having a very similar problem to what was described in the Ruby SDK (Invoice API - Ruby SDK - DELETE)

If I make a call using the API Explorer, everything works fine, but if I make a similar call using the PHP SDK I get a Reponse Code 415 - Unsupported type.

Here is a sample call from the SDK:
```
content-length: 0
content-type: text/plain; charset=utf-8
square-version: 2023-01-19
accept-encoding: deflate, gzip, br
accept: application/json
user-agent: Square-PHP-SDK/25.1.0.20230119 (2023-01-19) Zend/4.0.27 (Linux-4.18.0-348.2.1.el8_5.x86_64)

```

And the response from the server:

```
date: Sun, 29 Jan 2023 02:01:58 GMT
transfer-encoding: chunked
vary: Accept
x-frame-options: SAMEORIGIN
connection: keep-alive
content-type: text/plain; charset=utf-8
cache-control: no-cache

```

* *Unrecognizedcontent-type="text/plain",* *supported: application/x-protobuf,* *application/json* *

In the other thread, it seems the issue was fixed but I guess the bug is still present in the PHP SDK.

Hey @bhoward! Thanks for flagging this for us. We’ve identified an issue with how the PHP SDK is setting the content-type. We’ve got a long term fix in the works, but in the mean time you can work around this by making some manual changes to square/square/src/Apis/InvoicesApi.php.

This…

    public function deleteInvoice(string $invoiceId, ?int $version = null): ApiResponse
    {
        $_reqBuilder = $this->requestBuilder(RequestMethod::DELETE, '/v2/invoices/{invoice_id}')
            ->auth('global')
            ->parameters(TemplateParam::init('invoice_id', $invoiceId), QueryParam::init('version', $version));

        $_resHandler = $this->responseHandler()->type(DeleteInvoiceResponse::class)->returnApiResponse();

        return $this->execute($_reqBuilder, $_resHandler);
    }

Should be changed to this…

    public function deleteInvoice(string $invoiceId, ?int $version = null): ApiResponse
    {
        $_reqBuilder = $this->requestBuilder(RequestMethod::DELETE, '/v2/invoices/{invoice_id}')
            ->auth('global')
            ->parameters(
                TemplateParam::init('invoice_id', $invoiceId), 
                QueryParam::init('version', $version), 
                HeaderParam::init('Content-Type', 'application/json')
            );

        $_resHandler = $this->responseHandler()->type(DeleteInvoiceResponse::class)->returnApiResponse();

Note the HeaderParam::init('Content-Type', 'application/json') change. Apologies for the inconvenience!

Thanks Josh,

Your fix works :slightly_smiling_face: It now sends the request as a json instead of plain text.

content-length: 0
content-type: application/json
square-version: 2023-01-19
accept-encoding: deflate, gzip, br
accept: application/json

1 Like

Also for what its worth, same issue with getInvoice, which I solved by adding the same Content-Type definition to the function in the Square API.