我使用 Symfony HttpClient 来调用外部 api。当 statusCode 为 200 时,我可以使用getContent()方法来检索 API 响应。如果 API 响应为 400,则抛出 ClientException 并且我无法获取外部 API 消息。
$httpClient = HttpClient::create();
$response = $httpClient->request($method, $url);
if (200 !== $response->getStatusCode()) {
$apiResponse['statusCode'] = $response->getStatusCode();
$httpInfo = $response->getInfo();
$content = $response->getContent(); //this throws ClientException
}
Run Code Online (Sandbox Code Playgroud) 我有一个在 Silex Framework 中开发的小型后端。我尝试使这个请求在 POST 上工作:
但是当使用挂载操作时,只有这个请求有效:
如您所见,我必须在请求中添加一个额外的尾部斜杠。
以下是一些无法按预期工作的代码:
//index.php
$app->mount("/other", new FeedbackOther());
//FeedbackOther.php
$feedbackOther->get("/", "FeedbackOtherController::index")->bind('other');
$feedbackOther->post("","FeedbackOtherController::store");
Run Code Online (Sandbox Code Playgroud)
如果我做类似的事情
//index.php
$app->post('/other', "FeedbackOtherController::store");
$app->mount("/other", new FeedbackOther());
//FeedbackOther.php
$feedbackOther->get("/", "FeedbackOtherController::index")->bind('other');
Run Code Online (Sandbox Code Playgroud)
POST 请求无需附加斜杠即可工作,但在这种情况下,我看不到使用挂载操作的意义。
我也试过使用 .htacces 重写,但重写规则在 GET 中转换了 POST 请求。