Skip to content
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 set headers #6

Open
aichy126 opened this issue Jul 2, 2018 · 4 comments
Open

how to set headers #6

aichy126 opened this issue Jul 2, 2018 · 4 comments

Comments

@aichy126
Copy link

aichy126 commented Jul 2, 2018

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

@OrkhanAlikhanov
Copy link
Member

OrkhanAlikhanov commented Jul 2, 2018

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
}

@aichy126
Copy link
Author

aichy126 commented Jul 2, 2018

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

@aichy126
Copy link
Author

aichy126 commented Jul 2, 2018

sorry i see it

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

thanks ~~
haha

@OrkhanAlikhanov
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants