How do you add CORS headers in Redstone interceptor?

后端 未结 1 1424
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 15:57

I\'m trying to add CORS headers to incoming requests but I\'ve noticed that app.response.headers is an immutable map and app.request.response doesn\'t

相关标签:
1条回答
  • 2021-01-21 16:34

    I found the fix in the first piece of code inside the Interceptor documentation...:)

    @app.Interceptor(r"/api/.*", chainIdx: 1)
    corsInterceptor() {
        if (app.request.method == "OPTIONS") {
            var response = new shelf.Response.ok("", headers: HEADERS);
            app.chain.interrupt(statusCode: HttpStatus.OK, responseValue: response);
        } else {
            app.chain.next(() => app.response.change(headers: HEADERS));
        }
    }
    

    app.chain.next() can take a callback as argument, which is expected to return a Response object. In this case app.response.change() returns a response with the correct headers.

    0 讨论(0)
提交回复
热议问题