因此,这是一个应该接受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