Skip to content

Clean GET requests sending serialized empty JSON array as content body, breaking Directus Cloud #10

@mglinski

Description

@mglinski

When making a get() request to the configured API server, the current way data is being passed into Guzzle is making Guzzle send a content-body with GET requests. I assume this is also happening for other HTTP Verbs that should generally not have content bodies in their request.

This is due to the 'json' request option being sent to guzzle populated with an empty array by default on all requests where no attributes values have been setup. Guzzle is passing along this empty array as a request body and producing the correct headers/body content in these requests.

I am running PHP v8.1, Composer resolved guzzlehttp/guzzle to v7.5.0 and c14r/directus-api to dev-master.

When I modify the protected function buildOptions(): array function to the following code, this no longer happens:

/**
 * Prepares the request parameters
 *
 * @return array
 */
protected function buildOptions(): array
{
    return [
        'headers' => $this->headers,
        'query'   => $this->query,
        'json'    => !empty($this->attributes) ? $this->attributes : null
    ];
}

Here is an example raw request/response logged via debug proxy from this package making a request to a Directus app hosted on Directus Cloud:

Request:

GET /collections HTTP/1.1
Host: XXXXXXXXXXXXXXXXXXXXXXXXXXXX
User-Agent: GuzzleHttp/7
Content-Type: application/json
Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Content-Length: 2

[]

Response:

HTTP/1.1 403 Forbidden
Server: CloudFront
Date: Fri, 02 Dec 2022 18:48:01 GMT
Content-Type: text/html
Content-Length: 915
X-Cache: Error from cloudfront
Via: 1.1 XXXXXXXXXXXXXXXXXXXX.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: XXX-XX
X-Amz-Cf-Id: XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Connection: close

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: XXXXXXXXXXXXXXXXXXXXXXXXXXXX
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions