因此,這是一個應該接受POST請求下列參數的API:如何將JSON請求和表單數據請求一起發送?
token (as form data)
apiKey (as form data)
{
"notification": {
"id": 1,
"heading": "some heading",
"subheading": "some subheading",
"image": "some image"
}
} (JSON Post data)
現在我的問題是,我不能夠在同一個POST請求的表單數據和JSON數據一起發送。因爲,表單數據使用Content-Type: application/x-www-form-urlencoded
和JSON需要有Content-Type: application/json
我不知道如何將它們都發送到一起。我正在使用郵差。
編輯:
因此API會調用該函數create
,我需要做這樣的事情:
public function create() {
$token = $this -> input -> post('token');
$apiKey = $this -> input -> post('apiKey');
$notificationData = $this -> input -> post('notification');
$inputJson = json_decode($notificationData, true);
}
但不是我不能夠得到JSON數據和表格數據一起。
我不得不這樣做是爲了獲得JSON數據僅
public function create(){
$notificationData = file_get_contents('php://input');
$inputJson = json_decode($input, true);
} // can't input `token` and `apiKey` because `Content-Type: application/json`
請發表您的代碼。 –
您是否嘗試過直接發送JSON而不將內容類型設置爲'application/json'? –
它看起來像一個表單帖子,其中3個鍵具有3個字符串值。 – jeroen