i am trying to do a get request with angular 6 to an API on a production server and i sent an authorization token header together with it but i keep getting
- XHR failed loading: OPTIONS "http://myserver/api/endpoint"
- OPTIONS "http://myserver/api/endpointnet ::ERR_EMPTY_RESPONSE"
- message: "Http failure response for (unknown url): 0 Unknown Error"
Error
Here is my code
authheader = {
headers: new HttpHeaders({
'Authorization': 'Bearer '+this.tokenr,
'Content-Type': 'text/plain'
})
}
public async getUserInfo() {
return new Promise(async resolve => {
this.http.get(this.realapi + '/user', this.authheader)
.map(data => <any>data)
.subscribe(data => {
resolve(data);
console.log(data, 'data here');
}, err => {
console.log(err,'errorrr');
});
});
}
I am supposed to use this endpoint to fetch the user details after successful sign in from a laravel(passport) 5.7 api
Below is how i am handling my CORS and Preflight request in laravel
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Response;
class PreflightResponse
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next )
{
if ($request->getMethod() == "OPTIONS") {
return Response::make('OK', 200, $headers);
}
$response = $next($request);
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE, OPTIONS');
$response->header('Access-Control-Allow-Headers', 'Content-Type, Authorization, application/json');
return $response;
}
}
I added it to the $protected middleware(so it passes through all the request) in the kernel.php file
Funny enough, the POST http request works but the GET doesn't :(
POST request carries the data/parameter and 'Content-Type': 'application/x-www-form-urlencoded' header but the GET request just using the 'Authorization'; 'Bearer '+token