Ad

REST Postman Request

- 1 answer

I have a REST POST request that should the following format:

{
"data" : {
  "type" : "studies",
   "attributes" : {
   "age": "9999",
    "name": "XYZ"
  }
 }
}

I want to be able to upload a file with the request. so my request body looks like this image.

My Post request

The thing is, in my controller, I'm not able to detect anything.

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 
}

Unfortunately, my controller is not able to detect these attributes. How should I input them in Postman?

Ad

Answer

Dot delimeter will convert into _. So, you can have :

{
    "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')

If you still have an empty page or error 519 issue, you need to put CSRF token in Cookie, or except it.

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 = [
        '*',
    ];
}
Ad
source: stackoverflow.com
Ad