James Ikeler James Ikeler - 5 months ago 54
Swift Question

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>")

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")


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>")


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