Swift-POST请求,发送带有Vapor 3的JSON

Swift-POST请求,发送带有Vapor 3的JSON,json,swift,http,vapor,Json,Swift,Http,Vapor,我在使用Vapor 3发送POST请求时遇到问题,请求的正文中包含JSON。我正在使用它来测试它,在这里它用发送的相同JSON进行响应 我在上查看了答案,但在编码和内容类型方面出现了错误 router.get(“hooray”){req->Future in var postHeaders:HTTPHeaders=.init() 添加(名称:.contentType,值:“应用程序/json”) postHeaders.add(名称:。接受,值:“*/*”) 添加(名称:.acceptEncod

我在使用Vapor 3发送POST请求时遇到问题,请求的正文中包含JSON。我正在使用它来测试它,在这里它用发送的相同JSON进行响应

我在上查看了答案,但在编码和内容类型方面出现了错误

router.get(“hooray”){req->Future in
var postHeaders:HTTPHeaders=.init()
添加(名称:.contentType,值:“应用程序/json”)
postHeaders.add(名称:。接受,值:“*/*”)
添加(名称:.acceptEncoding,值:“gzip,deflate”)
让oneField=singleGet(foo:barfoobar)
//{foo:“barfoobar”}-JSON字符串
let encoder=JSONEncoder()
编码器.outputFormatting=.Prettypted
让jsonData=try encoder.encode(oneField)
让jsonString=String(数据:jsonData,编码:.utf8)!
让postBody=HTTPBody(字符串:jsonString)
让httpReq=HTTPRequest(方法:.POST,url:“/POST”)
让httpRes=HTTPClient.connect(主机名:“postman echo.com”,on:req)
.flatMap(to:singleGet.self){client in
req.http.headers=postHeaders
req.http.contentType=.json
req.http.body=postBody
返回client.send(httpReq.flatMap)(到:singleGet.self){resin
返回try-req.content.decode(singleGet.self)
}
}
返回try req.view().render(“test”,httpRes)
}
struct singleGet:Content{
让foo:String
}
我用这段代码得到了正确的响应,但我想知道当我重新编写代码以匹配时,为什么会出现错误

我已经在请求中添加了标题和正文,并在闭包中对它们进行了注释,但这样做行不通

让httpReq=HTTPRequest(方法:.POST,url:“/POST”,标题:postHeaders,正文:postBody)

如果您可以添加您收到的错误,它可能有助于调试问题:)