Reputation: 558
I am trying to send an HTTP request using Vapor, to verify a recaptcha
Google's Captcha api is defined as follows:
URL: https://www.google.com/recaptcha/api/siteverify METHOD: POST
POST Parameter | Description |
---|---|
secret | Required. The shared key between your site and reCAPTCHA. |
response | Required. The user response token provided by the reCAPTCHA client-side integration on your site. |
remoteip | Optional. The user's IP address. |
So I need to make a POST request with 2 parameters (secret and response).
In Swift i have:
func routes(_ app: Application throws {
app.on(.POST, "website_form") { req -> EventLoopFuture<View> in
var form: FromRequest = /*initial values*/
/*decode form data*/
do {
req.client.post("https://www.google.com/recaptcha/api/siteverify") { auth_req in
try auth_req.content.encode(CaptchaRequestBody(secret: "6Lfoo98dAAAAALRlUoTH9LhZukUHRPzO__2L0k3y", response: form.recaptcha_response), as: .formData)
auth_req.headers = ["Content-Type": "application/x-www-form-urlencoded"]
}.whenSuccess { resp_val in
print("Response: \(resp_val)")
}
}
}
/* More code */
}
struct CaptchaRequestBody: Content {
let secret: String
let response: String
}
After running the post request, I get following error code:
{
"success": false,
"error-codes": [
"missing-input-secret"
]
}
I can not find any solution that works, even the official Vapor docs were of no use, could someone please help me?
Upvotes: 2
Views: 1477
Reputation: 5180
The Google API requires requests to be URL-encoded forms. By using the .formData
setting, you are enforcing MultiPart.
Change the setting to .urlEncodedForm
, which ensures the request conforms to the Google API requirement.
Upvotes: 3
Reputation: 558
As Nick stated: the problem was that instead of .formData
, I needed to use .urlEncodedForm
.
Upvotes: 1