简体   繁体   中英

REST and content-type

I have a question is it possible to detect content type of request, I'm building REST service and tried $_SERVER['CONTENT_TYPE'] but I get undefined index error. I need this to determine with content type I should accept and which to be rejected.

If you're looking for request type - GET, POST, PUT, DELETE - it's $_SERVER["REQUEST_METHOD"] .

From your question I can't actually determine if you're talking about request type of something else.

Edit.

Content-type header is usually NOT sent in request. It's not mandatory in HTTP protocol. And even if it is sent, you can't rely on it - just like on any other data that comes from user. It can be fake.

Anyway you can get all request headers with getallheaders() PHP function.

http://php.net/getallheaders

我的解释是,您想要响应请求不可接受的HTTP状态代码4XX ,请看一下这个问题。

You can try headers_list() .

Example:

<?php
header('Content-Type: application/json');

$headers = headers_list();

var_dump($headers);
?>

OUTPUT:

array(2) {
  [0]=>
  string(23) "X-Powered-By: PHP/5.4.5"
  [1]=>
  string(24) "Content-Type: application/json"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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