简体   繁体   English

方法PUT的http_response_code问题

[英]http_response_code issue with method PUT

This issue is simple to understand, I'm coding an API and trying give a HTTP response, I'm testing on resttest . 这个问题很容易理解,我正在编写API并尝试给出HTTP响应,我正在resttest上进行测试。

The issue: I'm geting an error when method is PUT or DELETE trying return a HTTP response code over 400. 问题:当方法为PUT或DELETE尝试返回超过400的HTTP响应代码时,出现错误。

Here is my code: 这是我的代码:

.httacces: .httacces:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ webroot/index.php [QSA,L]

Here webroot/index.php: 这是webroot / index.php:

<?php
session_start();
error_reporting(E_ALL);

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");

http_response_code(400);
?>

just remembering, this same code works if method is GET or POST or if the code is under 400. 请记住,如果方法是GET或POST或代码小于400,则相同的代码也适用。

Trying this out... 试试这个...

I think the REST testing tool you are using will use CORS to figure out if it's allowed to make requests. 我认为您使用的REST测试工具将使用CORS来确定是否允许发出请求。

The issue is that for any non-safe HTTP request ( PUT , POST , DELETE , etc) CORS will first do what's called a 'pre-flight' request. 问题在于,对于任何非安全的HTTP请求( PUTPOSTDELETE等),CORS都会首先执行所谓的“飞行​​前”请求。

This pre-flight request is actually an OPTIONS request with several CORS headers and basically asks the server, "am I allowed to execute this next http method?". 这个飞行前请求实际上是一个带有几个CORS标头的OPTIONS请求,基本上是询问服务器:“我可以执行下一个http方法吗?”。

Your pre-flight fails, because the OPTIONS request will result in a 400 Bad request . 您的预检失败,因为OPTIONS请求将导致400 Bad request As a result the 'real' request will never be executed. 结果,“真实”请求将永远不会执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM