Http.swift
Http.swift copied to clipboard
how to set headers
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
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
}
so how can i set http server header? thanks~~
sorry i see it
return .ok("hello world!", headers: ["Server":"ios","test":"headers"])
thanks ~~ haha
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!