2
votes

My Guzzle POST request to https://api.scarif.dev/auth gives back a 404, while the page exists through Postman, or browser, or javascript. It should return a 200 with a 401 message, but Guzzle gives back a 404. In both POST and GET mode that is.

I've tried multiple Client setups, including different headers and disabling SSL verification, but without any success. Now I've copied the exact same headers that made it work in postman, but still no success.

I've been searching through google and stackoverflow, but couldn't find an answer that fixed my problem.

Request in PHP:

<?php
$client = new Client([
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
]);

$response = $client->request('POST', 'https://api.scarif.dev/auth', [
    'form_params' => []
]);

echo $response->getBody()->getContents();
?>

Expected result:

{
    "detail": "https://login.scarif.dev",
    "status": 401,
    "title": "Unauthorized",
    "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"
}

Actual result:

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.scarif.dev/auth resulted in a 404 Not Found response: 404 Not Found

Not Found (truncated...) in /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response)) 2 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(156):

GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 /home/admin/domains/login.scarif.dev/framework/ven in /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

API endpoint controller:

<?php

namespace Controller;

use Core\Config;
use Core\Request;
use Core\Response;
use Model\Token;
use Model\User;
use MongoDB\BSON\UTCDateTime;

class AuthController extends Controller
{
    public function view(User $user, Token $token)
    {
        extract(Request::getPostData());

        if (isset($access_token) && !empty($access_token)) {
            $_token = $token->getTokenByToken($access_token);

            if (
                $_token['type'] !== Token::TYPE_ACCESS_TOKEN ||
                $_token['expires_on'] <= new UTCDateTime()
            ) {
                return $this->view->display('json', [
                    'payload' => Response::apiResponse(
                        $this->config->get('url.login'), 401
                    )
                ]);
            }

            $token->delete($_token['_id']);

            $newToken = $token->create(Token::TYPE_ACCESS_TOKEN, $_token['user_id']);

            return $this->view->display('json', [
                'payload' => Response::apiResponse($newToken['token'])
            ]);
        }

        if (!isset($email) || !isset($password) || empty($email) || empty($password)) {
            return $this->view->display('json', [
                'payload' => Response::apiResponse(
                    $this->config->get('url.login'), 401
                )
            ]);
        }

        if (!$user->checkCredentials($email, $password)) {
            return $this->view->display('json', [
                'payload' => Response::apiResponse(
                    "The email address or password you've entered is invalid. Please check your entry and try again.",
                    422
                )
            ]);
        }

        $user = $user->getUserByEmail($email);
        $token = $token->create(Token::TYPE_ACCESS_TOKEN, $user['_id']);

        return $this->view->display('json', [
            'payload' => Response::apiResponse($token['token'])
        ]);
    }
}
1

1 Answers

0
votes

It seems like the issue is coming from the API you are consuming. When using your code with a different url it works just fine:

$client = new Client([
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
]);

$response = $client->request('POST', 'https://jsonplaceholder.typicode.com/posts', [
    'form_params' => []
]);

echo $response->getBody()->getContents();

Could you show the code for the API endpoints?