How to handle cross origin?
Why do we do that?
Browser will poke cross site server with OPTIONS method to determine the access right.
If browser don't see the right header, then it won't take the data.
If server side don't handle
OPTIONSmethod correctly, a server side exception may be raised.
REST
Repository : https://github.com/ccapeng/bookstore_openapi
To handle corss site origin
requestandresponse, implement middlewarecorsapp.middleware.CorsMiddlewareclass CorsMiddleware(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) response["Access-Control-Allow-Origin"] = "*" response["Access-Control-Allow-Headers"] = "*" response["Access-Control-Allow-Methods"] = "*" return responseAnd adjust settings in
bookstore_openapi.settings.pyMIDDLEWARE = [ ... 'corsapp.middleware.CorsMiddleware', ]To bypass cross origin, 3 access control attributes were added to
response.
GraphQL
Repository : https://github.com/ccapeng/bookstore_graphQL
To handle corss site origin
requestandresponse, implement middlewarecorsapp.middleware.CorsMiddlewareAnd adjust settings in
bookstore_graphql.settings.pyTo bypass cross origin, 3 access control attributes were added to
response.
gRPC
It's handled by proxy Envoy.
File : envoy-bookstore-grpc.yaml
Last updated
Was this helpful?