I'm having issues with the CORS feature. With the ...
# ktor
m
I'm having issues with the CORS feature. With the following:
Copy code
install(CORS) {
    anyHost()
  }
A
GET
request works just fine, but a
POST
doesn't. Trying to be a bit more explicit with this:
Copy code
install(CORS) {
    anyHost()
    allowHeader(HttpHeaders.ContentType)
    allowMethod(HttpMethod.Get)
    allowMethod(<http://HttpMethod.Post|HttpMethod.Post>)
  }
results in the
GET
not working either now. What's the minimum to get both
GET
and
POST
working from any host?
a
If I make the following request the server replies with the
Access-Control-Allow-Origin: *
header:
Copy code
curl -v -X POST --header 'origin: <http://localhost:3333>' <http://127.0.0.1:3333/>
Copy code
embeddedServer(Netty, port = 3333) {
    install(CORS) {
        anyHost()
        allowHeader(HttpHeaders.ContentType)
        allowMethod(HttpMethod.Get)
        allowMethod(<http://HttpMethod.Post|HttpMethod.Post>)
    }

    routing {
        get("/") {
            call.respondText { "get" }
        }
        post("/") {
            call.respondText { "post" }
        }
    }
}.start(wait = true)
m
Could you try on 0.0.0.0 rather than 127.0.0.1?
a
Do you mean for the origin, request URL or server host?
m
Request URL and server host is the set up I have
a
Copy code
embeddedServer(Netty, port = 3333, host = "0.0.0.0") { // ...
Curl:
Copy code
curl -v -X POST --header 'origin: <http://localhost:3333>' <http://0.0.0.0:3333/>
Response headers:
Copy code
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Content-Length: 4
< Content-Type: text/plain; charset=UTF-8
m
Mhhh I'm not in front of my laptop anymore right now, I'll try those same curl calls to try and make sense of this, thanks
OK, I was definitely missing
allowHeader(HttpHeaders.ContentType)
the first time I tried, but I have no idea why it didn't work when I tried with it just before posting here - now it works fine with that. Sorry for the trouble...