Home > other >  422 Unprocessable Entity when posting to Laravel
422 Unprocessable Entity when posting to Laravel

Time:08-16

I cannot figure out how to solve this.

Response is showing me Content-Type: application/json instead of multipart/form-data....

did anyone know what i need to do?

I build in .net 6 MAUI, Android 12 and RestSharp as http client

Please give a look on this image: (IMAGE) Response StatusCode

var client = new RestClient();
var request = new RestRequest(PostImageUrl, Method.Post);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddFile("image", bauzeichnung);
var response = client.Execute(request);

Everything is tested with Postman and works like expected.

EDIT also tried:

 request.AlwaysMultipartFormData = true;

Is this Directory Path correct? "/data/user/0/com.Lippert.Digital/cache/b81fe7a766a64981918f1012d7865c8c.jpg"

Can AddFile work with this type of path from my Android phone? This picture was taken with MediaPicker.Default.CapturePhotoAsync();

(IMAGE) Directory Path

EDIT

PHP Controller

    public function uploadImage(Request $request)
{
    $this->validate($request, [
        'image'     => 'file',
    ]);

    
    if ($request->file('image')) 
    {
        $name = time().$request->file('image')->getClientOriginalName();
        $request->file('image')->move('Bauzeichnungen',$name);

        $image = url('Bauzeichnungen/'.$name);
        
    }else
    {   
        $image = 'Image not found';
       
    }
    date_default_timezone_set('Europe/Berlin');
    DB::table('Bauzeichnung')->insert([  
        'image' => "$image",
        'erstellt_von' => 26,
        'aktualisiert_von' => 26,
        'Zeitstempel' => date('Y-m-d H:i:s'),
    ]);
    
}

PHP Route

Route::post('/uploadImage','ImageController@uploadImage');

CodePudding user response:

Directory Path is not correct, this path is belong to your cache storage. When you upload an image, image is writing your cache and when you post is, you are posting your image in cache. And when you try to take path, it giving cache path to you.

CodePudding user response:

422 Unprocessable Entity - Solution

After couple of days i found the Solution for the error above...


The reason for that:

Images are too big and only smaller values were allowed in php.ini.


How it can be solved:

  1. open your php.ini - (location: /etc/php/8.1/fpm/php.ini)

  1. set memory_limit = (i set to -1)

Sets the maximum amount of memory, in bytes, that a script can use. This can be used to prevent badly written scripts from "eating up" all of the available memory on a server. To set no memory limit, set this directive to the value -1.


  1. Set post_max_size = (i set to 128M)

Sets the maximum allowed size of POST data. This option also affects file upload. To upload larger files, the value must be greater than upload_max_filesize . In general, memory_limit should be greater than post_max_size. If an int value is used, that value is measured in bytes. You can also use the shorthand form as described in this FAQ . If the size of the POST data is greater than post_max_size, the $_POST and $_FILES superglobals become to be empty. This can be tracked in a number of ways, e.g. B. by passing the $_GET variable to the script that processes the data, ie and then checking if $_GET['processed'] is set.


  1. Set upload_max_filesize = (i set to 64M)

The maximum size that an uploaded file can have.


Where i got the Information - PHP Manual: https://www.php.net/manual/de/ini.core.php

Big thanks to : @Jason

  • Related