Berry Berry - 13 days ago 5
reST (reStructuredText) Question

How to handle PUT HTTP request in Vapor?

The only way I've found to update a record in Vapor is this:

drop.get("update") { request in

guard var first = try Acronym.query().first(),
let long = request.data["long"]?.string else {
throw Abort.badRequest
}
first.long = long
try first.save()
return first

}


However it's not a very RESTful way of doing it since it's performing a GET request with a parameter instead of a PUT request.

How does one perform a PUT request in Vapor?

Answer
drop.put("update") { request in
  guard var first = try Acronym.query().first(),
    let long = request.data["long"]?.string else {
    throw Abort.badRequest
  }

  first.long = long
  try first.save()

  return first
}