我有一个REST POST请求,该请求应采用以下格式:
{
"data" : {
"type" : "studies",
"attributes" : {
"age": "9999",
"name": "XYZ"
}
}
}
我希望能够随请求上传文件。所以我的请求正文看起来像这张图片。
事实是,在我的控制器中,我无法检测到任何东西。
if($request->has('data.attributes.name')) $name = $request->input('data.attributes.name')
if($request->has('data.attributes.age')) $name = $request->input('data.attributes.age')
if($request->hasFile('data.attributes.file')){
//Upload the file
}
很遗憾,我的控制器无法检测到这些属性。我应该如何在邮递员中输入它们?
点距将转换为_
。因此,您可以拥有:
{
"data_attributes_file": "file",
"data_attributes_name": "XYZ",
"data_attributes_age": "999"
}
$request->has('data_attributes_file')
$request->has('data_attributes_name')
$request->has('data_attributes_age')
如果仍然有空白页或错误519
问题,则需要将CSRF
令牌放入Cookie或其他除外。
app / Http / Middleware / VerifyCsrfToken
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* Indicates whether the XSRF-TOKEN cookie should be set on the response.
*
* @var bool
*/
protected $addHttpCookie = true;
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'*',
];
}