Server-Side Swift with Vapor, Episode 4: Accepting Data | Kodeco

In this episode, see how to use Codable to send and receive data with Vapor. Learn how to accept incoming data as JSON and return JSON responses.


This is a companion discussion topic for the original entry at https://www.kodeco.com/21451628-server-side-swift-with-vapor/lessons/4

I am using Insomnia as the API Client, also tried Postman… I am getting a 422 error
{
“reason”: “Unprocessable Entity”,
“error”: true
}


Not sure what I am missing,…

My routes.swift is:

import Vapor

func routes(_ app: Application) throws {
    app.get { req async in
        "It works!"
    }

    app.get("hello") { req async -> String in
        "Hello, world!"
      
    }
  
  app.get("hello", "vapor") { req -> String in
    return "Hello Vapor, oh and Hello Chuck!"
  }
  
  app.get("hello", ":name") { req -> String in
    guard let name = req.parameters.get("name") else {
      throw Abort(.internalServerError)
    }
    return "Hello, \(name)!"
  }
  
  app.post("info") { req -> String in
    let data = try req.content.decode(InfoData.self)
    return "Hello \(data.name)!"
  }
}

struct InfoData: Content {
 let name: String
}

Please advise

This is resolved, I was putting the parameters in as a query instead of the body…