3

How can I check if the Content-type of a POST request in PHP is either application/json or application/x-www-form-urlencoded?

I've tried using $_SERVER["CONTENT_TYPE"] and echo get_headers('url', 1)["Content-Type"] but neither of those work for me.

1
  • $_SERVER["CONTENT_TYPE"] must work Commented Jan 17, 2015 at 17:35

2 Answers 2

6
echo '<pre>'; print_r(getallheaders()); 

So

$allHeaders = getallheaders(); $contentType = $allHeaders['Content-Type']; 
Sign up to request clarification or add additional context in comments.

2 Comments

The first solution that actually works for me! Can't thank you enough!
No problem :) Be aware that you should always distrust user input and sanitise the input. The most commonly known abuse of this is SQL-injection. But headers are also being considered user-input.
1

It's working below codes. You can try it

$headerArray = getallheaders(); $contentType = $headerArray['Content-Type']; (Or) $contentType = $_SERVER["CONTENT_TYPE"]; (Or) $headerArray = apache_request_headers(); $contentType = $headerArray['Content-Type']; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.