Http.swift icon indicating copy to clipboard operation
Http.swift copied to clipboard

how to set headers

Open aichy126 opened this issue 7 years ago • 4 comments

func Webserver(){
    let server = Server()

    server.get("/hello/{id}") { request in
        request.headers = ["Server":"aichy","4":"12"]
        print(request.queryParams["state"]!)
        return .ok(request.routeParams["id"]!)
       // return
    }
  
    server.get("/"){
        request in
         request.headers = ["Server":"ios","test":"headers"]
        return .ok("hello world! ")
    }

    do{
        try server.run(port:2121)
    }
    catch{
        
    }
  
    // go to http://localhost:8080/hello/1?state=active in the browser
}

request.headers = ["Server":"ios","test":"headers"] Don't work~~thanks

aichy126 avatar Jul 02 '18 13:07 aichy126

request parameter is read-only. it's sent by the client. Server is supposed to return response with the headers you want.

server.get("/") {  request in
    return .ok("hello world!", headers: ["Server": "ios", "test": "headers"])
}

More verbose:

server.get("/") {  request in
    let response: Response = .ok("hello world!")
    response.headers = ["Server": "ios", "test": "headers"]
    return response
}

OrkhanAlikhanov avatar Jul 02 '18 13:07 OrkhanAlikhanov

so how can i set http server header? thanks~~

aichy126 avatar Jul 02 '18 13:07 aichy126

sorry i see it

return .ok("hello world!", headers: ["Server":"ios","test":"headers"])

thanks ~~ haha

aichy126 avatar Jul 02 '18 13:07 aichy126

Well, actually we need to have defaultHeaders somewhere so that changing "Server" header does not require to change every response.headers. I'm adding this as a feature request. Thank you!

OrkhanAlikhanov avatar Jul 02 '18 13:07 OrkhanAlikhanov