我使用了L5-摇摆 5.7.*包(Swagger-php的包装器),并尝试了描述Laravel。所以,我的代码如下:
/**
* @OA\Post(path="/subscribers",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* @OA\Property(property="email", type="string")
* )
* )
* ),
* @OA\Response(response=201,description="Successful created"),
* @OA\Response(response=422, description="Error: Unprocessable Entity")
* )
*/
public function publicStore(SaveSubscriber $request)
{
$subscriber = Subscriber::create($request->all());
return new SubscriberResource($subscriber);
}但是,当我尝试通过swagger面板发送请求时,我得到了代码:
curl -X POST "https://examile.com/api/subscribers" -H "accept: */*" -H "Content-Type: application/json" -H "X-CSRF-TOKEN: " -d "{\"email\":\"bademail\"}"如您所见,accept is not application/json和Laravel没有将其标识为AJAX请求。因此,当我发送错误的数据,并期望获得422与实际错误,我得到200个代码与错误在“会话”。通过swagger面板的请求(XHR)也被错误地处理,卷曲代码只是为了清晰。
另外,我发现在以前的版本中使用了如下内容:
* @SWG\Post(
* ...
* consumes={"multipart/form-data"},
* produces={"text/plain, application/json"},
* ...)但现在已经过时了。
那么,如果验证失败,如何在不重定向的情况下获得422代码?或者添加“XMLHttpRequest”标题?这里最好做什么?
发布于 2018-11-25 16:36:34
响应没有指定mimetype。
@OA\Response(response=201, description="Successful created"),如果您指定了json响应,swagger-ui将发送一个Accept: application/json头。
PS。由于json是如此常见的swagger-php有一个@OA\JsonContent速记,这适用于响应:
@OA\Response(response=201, description="Successful created", @OA\JsonContent()),请求者:
@OA\RequestBody(
@OA\JsonContent(
type="object",
@OA\Property(property="email", type="string")
)
),发布于 2022-03-30 08:11:51
您可以使用这个,我使用请求类,对文件请求。
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
public function rules()
{
return [
'currentPassword' => 'required',
'newPassword' => 'required|same:confirmPassword',
'confirmPassword' => 'required'
];
}
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'errors' => $validator->errors(),
'status' => true
], 422));
}https://stackoverflow.com/questions/53168311
复制相似问题