Boryk
Boryk

Reputation: 68

Swift Vapor client is not sending Body on Post Request

I am attempting to make a request to an external API in Vapor using a .post() request on the client.

let payload = TwitchTokenPayload(client_id: client_id,
                                 client_secret: client_secret,
                                 grant_type: "client_credentials")

req.client.post("https://id.twitch.tv/oauth2/token", content: payload)

Payload correctly conforms to Content protocol, and yet I am still getting the 400 error back from Twitch saying missing Client Id.

I have set this same request up on Postman and it works.

One thing I noticed, I'm not sure if this is a bug with Vapor, but inside the Client file's send() function, they actually are always passing a nil body.

var request = ClientRequest(method: method, url: url, headers: headers, body: nil, byteBufferAllocator: self.byteBufferAllocator)

I think this may be what is causing the issue. Does anyone have an alternative way to work around this? Or should I just implement a standard HTTP request not using Vapor? This seems like a major issue with this framework if this is the case.

Upvotes: 2

Views: 80

Answers (1)

imike
imike

Reputation: 5646

There is another method which should work fine

let payload = TwitchTokenPayload(client_id: client_id,
                                 client_secret: client_secret,
                                 grant_type: "client_credentials")
req.client.post("https://id.twitch.tv/oauth2/token") { req in
    try req.content.encode(payload, as: .json)
}

Upvotes: 1

Related Questions