Swift - Vapor using HTML

I recently moved from Perfect to Vapor . In Perfect, you can do something similar without using an 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 to return html is to do this. How can I return the html code without using the html file in pairs?

 drop.get("/") { request in
    return try drop.view.make("somehtmlfile.html")
} 
+4
source share
1 answer

You can create your own Response by completely avoiding views.

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
}
+8
source

Source: https://habr.com/ru/post/1659962/


All Articles