Search code examples
laravelvalidationrequestmime-types

Why is checking for a json mimetype not working in my Request?


I have a Laravel API and I make a custom Request to validate the values. I'm testing my routes via Postman and I added a JSON file to my settings field. But when I try to validate the mimetype in my custom request an error gets thrown. When I remove |mimetypes:application/json the route returns a response successfully. Why is this happening?

public function rules()
{
    return [
        'name' => 'required|string|min:2|max:32|unique:game_templates',
        'path' => 'required|string|url|unique:game_templates', 
        'settings' => 'required|file|mimetypes:application/json',
        'orientation' => Rule::in(GameTemplate::$orientations),
    ];
}

enter image description here

The JSON content

{
    "name":"test"
}

Solution

  • Are you sure you are actually uploading a file with the application/json mimetype? A .json extension doesn't automatically mean it's application/json. Also note the following from the documentation:

    To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.