-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use CORS? #57
Comments
Sorry. |
@pmpmopm1 Not a problem. If you are using this in a development scenario you can send the following header back to the client:
If you want to make it a bit fancier, you can create a handler: public class HTTPCORSHandler: HTTPRequestHandler {
public func respond(to request: HTTPRequest, nextHandler: HTTPRequest.Handler) throws -> HTTPResponse? {
let response = try nextHandler(request)
// Add access control header for GET requests
if request.method == .GET {
response?.headers["Access-Control-Allow-Origin"] = "*"
}
return response
}
} For increased security you can add additional checks, dig down into the request, and send back different CORS headers for different clients. |
Could you pls show the code how to set the header. Thx |
@YiYiZheng I've added a section about CORS to the readme: If you have a response object, simply set the header with: response.headers["Access-Control-Allow-Origin"] = "*" |
I want to call "localhost" from another domain.
How to setting CORS?
The text was updated successfully, but these errors were encountered: