added example usage for API

This commit is contained in:
hrj 2021-08-04 11:45:03 +05:30
parent 6f6c6eda66
commit 1d26b76b01
1 changed files with 46 additions and 9 deletions

View File

@ -115,6 +115,9 @@ If a sufficient number of users agree on their answer to the unknown word, it is
***
## HTTP API
The service can be accessed using a simple HTTP API.
### - `/v1/captcha`: `POST`
- Parameters:
- `level`: `String` -
@ -136,7 +139,7 @@ If a sufficient number of users agree on their answer to the unknown word, it is
- `height`: `Int`
- `width`: `Int`
- Return type:
- Returns:
- `id`: `String` - The uuid of the captcha generated
@ -144,7 +147,7 @@ If a sufficient number of users agree on their answer to the unknown word, it is
- Parameters:
- `id`: `String` - The uuid of the captcha
- Return type:
- Returns:
- `image`: `Array[Byte]` - The requested media as bytes
@ -153,12 +156,46 @@ If a sufficient number of users agree on their answer to the unknown word, it is
- `id`: `String` - The uuid of the captcha that needs to be solved
- `answer`: `String` - The answer to the captcha that needs to be validated
- Return Type:
- Returns:
- `result`: `String` - The result after validation/checking of the answer
- True - If the answer is correct
- False - If the answer is incorrect
- Expired - If the time limit to solve the captcha exceeds
## Example usage
In javascript:
```js
const resp = await fetch("/v1/captcha", {
method: 'POST',
body: JSON.stringify({level: "easy", media: "image/png", "input_type" : "text"})
})
const respJson = await resp.json();
let captchaId = null;
if (resp.ok) {
// The CAPTCHA can be displayed using the data in respJson.
console.log(respJson);
// Store the id somewhere so that it can be used later for answer verification
captchaId = respJson.id;
} else {
console.err(respJson);
}
// When user submits an answer it can be sent to the server for verification thusly:
const resp = await fetch("/v1/answer", {
method: 'POST',
body: JSON.stringify({id: captchaId, answer: "user input"})
});
const respJson = await resp.json();
console.log(respJson.result);
```
***
## Roadmap