Mojtaba Barari
Mojtaba Barari

Reputation: 1274

How to get custom headers from axios post response?

i'm using nuxt/axios with laravel as my backend. in my responses from laravel i send a custom header named _msg but i cant access it. in my console.log(response) i get only this:

enter image description here

but in my brower network i get the header:

enter image description here

how can i access it?

UPDATED

added this to my laravel middleware: this is an example if request is from manager and admin

<?php

namespace App\Http\Middleware;

use Closure;
use App\Traits\UtilsTrait;

class ManagerPlus
{
    use UtilsTrait;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
//        return $next($request);
        if($this->isMoreManager()){
            $request->panelType = $this->addPanelType();
            $response = $next($request);

            $response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition');
            return $response;
        }
        return $this->permissionDenied();
    }
}


UPDATE AFTER EXPOSE:

i did as told with my laravel/fruitcake setting and middleware and this is my new header that i get from axios. but still not getting my _msg

enter image description here

Upvotes: 1

Views: 1681

Answers (1)

jjchiw
jjchiw

Reputation: 4445

What I think you need to do is:

if you are using the package https://github.com/fruitcake/laravel-cors you will have config/cors.php and there is where you should add

'exposed_headers' => ['_msg'],

and you have to create the middleware as it's explained in the issue https://github.com/fruitcake/laravel-cors/issues/308#issuecomment-490969761

$response->headers->set('Access-Control-Expose-Headers', '_msg');

I hope it works

Upvotes: 1

Related Questions