gizm0bill / angular2-rest-client

MIT License
8 stars 5 forks source link

Question: Requests OPTIONS instead of GET (Prevent CORS) #6

Closed asCii88 closed 6 years ago

asCii88 commented 7 years ago

Hello,

I have this really simple code

@BaseUrl('http://127.0.0.1:8000/api/v1')

@Injectable()
export class TestService extends AbstractApiClient {
    constructor( protected http: Http ) { super(http); }

    @Type( ResponseContentType.Json )

    @GET('/test') public readTest(): Observable<Response> { return }
}

But the request goes like this:

OPTIONS /api/v1/test HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: es-419,es;q=0.8,en;q=0.6,lt;q=0.4

I know that this is CORS, is there a way to just go straight to the actual request?

gizm0bill commented 7 years ago

It's the standard from cross origin requests (Host: 127.0.0.1:8000 from localhost:4200), it needs to do a preflight request, i.e. OPTIONS. Check here: http://stackoverflow.com/questions/36353532/angular2-options-method-sent-when-asking-for-http-get