rfc:parse_request_body_in_json

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:parse_request_body_in_json [2018/10/02 15:59] – created jhdxrrfc:parse_request_body_in_json [2018/10/02 16:57] (current) – fix typo jhdxr
Line 1: Line 1:
-====== PHP RFC: Add support ofr parsing request body in JSON format  ======+====== PHP RFC: Add support for parsing request body in JSON format  ======
   * Version: 0.1   * Version: 0.1
   * Date: 2018-10-02   * Date: 2018-10-02
Line 11: Line 11:
  
 ===== Proposal ===== ===== Proposal =====
-It's very common for javascript library to send JSON payload now, however, PHP doesn't have built-in support for it. Userland developers have to deserialize it by themselves, which is inconvenient and inefficient. This RFC proposes to reuse the ability from ext/json to deserialize the payload and fill in ''$_POST''.+It's very common for javascript library to send JSON payload now, however, PHP doesn't have built-in support for it. Userland developers have to deserialize it by themselves, which is inefficient and inconvenient. This RFC proposes to reuse the ability from ext/json to deserialize the payload and fill in ''$_POST''. 
 + 
 +For example, 
 +<code> 
 +curl -i -H "Content-Type: application/json" -X POST http://localhost:8000 -d '{"foo":"123","bar":456}' 
 +</code> 
 +will get 
 +<code php> 
 +var_dump($_POST); 
 +/* 
 +array(2) { 
 +  ["foo"]=> 
 +  string(3) "123" 
 +  ["bar"]=> 
 +  int(456) 
 +
 +*/ 
 +</code> 
 + 
 +Some edge cases: 
 +  - illegal data: If the payload cannot be decoded, ''$_POST'' will be an empty array. 
 +  - scalar type value: If the payload is a simple scalar type value, like numbers, string, TRUE or FALSE, ''$_POST'' will be an empty array. 
 +  - large number value: Large number will be stored as string, which means ''JSON_BIGINT_AS_STRING'' is enabled. There won't be an option/setting for this because this is how ''$_POST'' deal with large numbers. 
  
  
rfc/parse_request_body_in_json.1538495942.txt.gz · Last modified: 2018/10/02 15:59 by jhdxr