Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Vapor Using HTML

I recently made the switch from Perfect to Vapor.In Perfect you could do something like this without using a html file.

routes.add(method: .get, uri: "/", handler: {
    request, response in
    response.setHeader(.contentType, value: "text/html")
    response.appendBody(string: "<html><img src=http://www.w3schools.com/html/pic_mountain.jpg></html>")
    response.completed()
   }
)

In Vapor the only way I found to return the html is to do this.How could I return html code without using a html file in vapor?

 drop.get("/") { request in
    return try drop.view.make("somehtmlfile.html")
} 
like image 486
James Ikeler Avatar asked Nov 06 '16 02:11

James Ikeler


1 Answers

You can build your own Response, avoiding views completely.

drop.get { req in
  return Response(status: .ok, headers: ["Content-Type": "text/html"], body: "<html><img src=http://www.w3schools.com/html/pic_mountain.jpg></html>")
}

or

drop.get { req in
  let response = Response(status: .ok, body: "<html><img src=http://www.w3schools.com/html/pic_mountain.jpg></html>")
  response.headers["Content-Type"] = "text/html"
  return response
}
like image 173
tobygriffin Avatar answered Oct 22 '22 14:10

tobygriffin