James Ikeler
James Ikeler

Reputation: 121

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

Upvotes: 8

Views: 2299

Answers (1)

tobygriffin
tobygriffin

Reputation: 5421

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
}

Upvotes: 12

Related Questions