인증(Authentication)
GenderAPI에 대한 모든 API 요청은 인증이 필요합니다. 인증 방법은 두 가지입니다:
- POST 본문에
key
파라미터로 API 키 포함 - Authorization 헤더에 Bearer 토큰 방식으로 API 키 포함
1. POST 본문에 API 키 포함
가장 간단한 인증 방법은 POST 요청의 JSON 본문에 key
필드로 API 키를 포함하는 것입니다.
예를 들어 "Alice"라는 이름의 성별을 확인하려면:
cURL 예제
curl -X POST "https://api.genderapi.io/api" \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "key": "YOUR_API_KEY"}'
JavaScript fetch 예제
fetch("https://api.genderapi.io/api", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "Alice",
key: "YOUR_API_KEY"
})
})
.then(response => response.json())
.then(data => console.log(data));
Python requests 예제
import requests
url = "https://api.genderapi.io/api"
payload = {
"name": "Alice",
"key": "YOUR_API_KEY"
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
2. Authorization 헤더에 API 키 포함 (Bearer 토큰)
보다 보안적인 방법으로, Authorization 헤더에 Bearer 스킴을 사용하여 API 키를 전달할 수 있습니다. 이 방법에서는 JSON 본문에 API 키를 포함하지 않습니다.
대신 입력 파라미터만 (예: name
) POST 본문에 포함됩니다.
예를 들어 "Alice"라는 이름의 성별을 확인하려면:
cURL 예제
curl -X POST "https://api.genderapi.io/api" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"name": "Alice"}'
JavaScript fetch 예제
fetch("https://api.genderapi.io/api", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
},
body: JSON.stringify({
name: "Alice"
})
})
.then(response => response.json())
.then(data => console.log(data));
Python requests 예제
import requests
url = "https://api.genderapi.io/api"
payload = {
"name": "Alice"
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
두 방식 중 무엇을 선택할까?
두 인증 방식은 모든 API 엔드포인트에서 사용할 수 있습니다. 그러나 Authorization 헤더를 사용하는 방식은 요청 본문에 API 키를 노출하지 않으므로 더 보안적이며 로그에 키가 남지 않아 안전합니다.
어떤 방식을 사용하든 POST 요청 시 Content-Type
헤더를 반드시
application/json
으로 설정해야 합니다.