Доступ к параметрам тела запроса - PullRequest
0 голосов
/ 13 января 2019

Я пытаюсь создать цепочку запросов к стороннему API со следующим кодом:

import Vapor

final class APIController: RouteCollection {

    private let baseULR = "..."

    func boot(router: Router) throws {
        router.post("login", use: validate)
    }

    func validate(_ req: Request) throws -> Future<Response> {
        // Get the phone number of the user
        let phoneNumber = try req.content.syncGet(String.self, at: "phone_number")
        return try req.client().post("\(baseUrl)/...", beforeSend: { post in
            try post.content.encode(json: ["phone_number": phoneNumber])
        })
    }
}

Но при тестировании запроса я получаю сообщение об ошибке:

[ ERROR ] Abort.415: Unsupported Media Type (ContentCoders.swift:95)
[ DEBUG ] Suggested fixes for Abort.415: Register an `DataDecoder` using `ContentConfig`. Use one of the decoding methods that accepts a custom decoder. (ErrorMiddleware.swift:26)

Это, к сожалению, я не понимаю, как исправить.

1 Ответ

0 голосов
/ 14 января 2019

Вы можете попытаться предоставить Content-Type полезной нагрузки

func validate(_ req: Request) throws -> Future<Response> {
    // Get the phone number of the user
    return req.content.get(String.self, at: "phone_number").flatMap { phoneNumber in
        return try req.client().post("/...") {
            try $0.content.encode(["phone_number": phoneNumber], as: .json)
        }
    }
}
...