0
votes

I am trying to do a simple API post using Guzzle. The API however keeps returning the error "UnsupportedApiVersion [Message] => The requested resource with API version '1' does not support HTTP method 'GET'."

When doing a simple post through postman using Content-Type: application/json header and a simple body:

{
"Username" : "xxxxxxx",
"Password" : "xxxxxxx",
"ApplicationID" : "xxxxxxx",
"DeveloperID" : "xxxxxxx"
}

It works fine and i get a result back as expected.

However when using the following code i keep getting a method GET is not supported error.


public function connect()
{
   $client = new Client([
      'base_uri' => $this->url,
      'headers' => [
          'Accept' => 'application/json',
          'Content-Type' => 'application/json',
      ],
      'http_errors' => $this->getHttpErrors(),
    ]);
    return $client;
}

public function login()
{
    $client = $this->connect();
    $res = $client->post($this->url.'auth/signin', [
        'json' => [
            'ApplicationID' => xxxxxx,
            'DeveloperID'   => xxxxxx,
            'Username' => xxxxxx,
            'Password' => xxxxxx
        ]
    ]);

    $results = json_decode($res->getBody());
    return $results;
}

Instead of using 'json' i have tried 'form_params' which gives me the same result.

I am using Guzzle 6.3.3

1
Difficult to pinpoint exactly what could be going wrong, but have you tried replacing the key json with query? - dearsina
Using query unfortunately gives me the same result - Renee Thomassen

1 Answers

1
votes

A few issues:


"UnsupportedApiVersion [Message] => The requested resource with API version '1' does not support HTTP method 'GET'

This points to an issue with a mismatched request - where a GET was sent instead of a POST, which points to either an issue with the underlying mechanism that Guzzle uses (cURL, PHP stream, or custom), or something in the request that is coercing Guzzle into making a GET. Have you checked to see if this is indeed happening and the API is reporting accurately? You can either var_dump($res); to check, or by forming the request as a separate variable via $req = client->createRequest('post',...) and then checking $req->getMethod() after sending the request, based on this StackOverflow QA.

Looking at this thread, it looks like redirects are a common cause for this happening - for example if the URL that you have in your PHP is different from the one that is working in Postman, and it has a typo in it. You could also try disabling redirects from happening ever, by setting the option with Guzzle:

$res = $client->post($this->url.'auth/signin', [
    'json' => [
        'ApplicationID' => xxxxxx,
        'DeveloperID'   => xxxxxx,
        'Username' => xxxxxx,
        'Password' => xxxxxx
    ],
    'allow_redirects' => false
]);

As a side note, the point of base_uri is to make it so all you have to do is specify the path when you call a request method. Since you have already defined the base_uri as $this->url, you could turn this:

$res = $client->post($this->url.'auth/signin', ...

into:

$res = $client->post('auth/signin', ...

Also, be careful about the above, as this is actually an easy way to form malformed URLs - especially since you didn't share what the value of $this->url is in your code.


Also, you mentioned trying the request with form_params. Make sure to also swap out the Content-Type header when doing so - e.g. set to application/x-www-form-urlencoded.