GuzzleHTTP在现有页面上返回404
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GuzzleHTTP在现有页面上返回404相关的知识,希望对你有一定的参考价值。
我向Guzxswpoi发出的Guzzle POST请求返回404,而该页面通过Postman,浏览器或javascript存在。它应该返回一个带有401消息的200,但Guzzle返回404.在POST和GET模式下都是。
我尝试过多个客户端设置,包括不同的标头和禁用SSL验证,但没有任何成功。现在我复制了完全相同的标题,使其在邮递员中工作,但仍然没有成功。
我一直在搜索谷歌和stackoverflow,但找不到解决我的问题的答案。
请求php:
https://api.scarif.dev/auth
预期结果:
<?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();
?>
实际结果:
致命错误:未捕获GuzzleHttp Exception ClientException:客户端错误:
{ "detail": "https://login.scarif.dev", "status": 401, "title": "Unauthorized", "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" }
导致POST https://api.scarif.dev/auth
响应:404 Not FoundNot 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): GuzzleHttpExceptionRequestException::create(Object(GuzzleHttpPsr7Request), Object(GuzzleHttpPsr7Response)) #1 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttpMiddleware::GuzzleHttp{closure}(Object(GuzzleHttpPsr7Response)) 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)/ home / admin / domains / login中的#3 /home/admin/domains/login.scarif.dev/framework/ven。第113行的scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php
API端点控制器:
404 Not
Found
似乎问题来自您正在使用的API。使用不同网址的代码时,它可以正常工作:
<?php
namespace Controller;
use CoreConfig;
use CoreRequest;
use CoreResponse;
use ModelToken;
use ModelUser;
use MongoDBBSONUTCDateTime;
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'])
]);
}
}
你能展示一下API端点的代码吗?
以上是关于GuzzleHTTP在现有页面上返回404的主要内容,如果未能解决你的问题,请参考以下文章
获取 JWT 后,带有 Spring Boot 后端的 Angular 在现有路由上返回 404