API를 테스트할 때 users.json을 사용했습니다. Postman으로 User를 생성하면 잘 작동하며, 기본적으로 활성화되어 active=true가 정상적으로 적용됩니다.
하지만 Node.js로 코드를 작성해 사용자를 생성할 때는 동일한 파라미터를 사용해도 active=true가 작동하지 않습니다. 그런데도 성공 메시지는 반환되는데, 내용은 다음과 같습니다.
success: true,
active: false,
왜 이런 일이 발생하는 걸까요? 백엔드 코드에서도 해당 값을 확인조차 할 수 없습니다.
david
(David Taylor)
12월 3, 2019, 10:25오전
2
내 추측으로는 node.js 코드가 API 키를 올바르게 전달하지 않아서 active 매개변수를 덮어쓰지 못하게 되고 있는 것 같습니다. 여기에서 코드 일부를 공유해 주시면 도움을 드릴 수 있을지도 모릅니다.
데이비드님, 안녕하세요. 감사합니다:
제 요청은 다음과 같습니다:
{
"url": "users.json",
"method": "post",
"data": {
"api_key": "57d06a163190ee90de1118ac2adbaf5eeb5aa93d4d02dbe8f5d424c388f126e294c",
"api_username": "Nathan001",
"name": "tokgood",
"email": "tokgood@qq.com",
"password": "64c6457d-b815-4e87-8ea1-e66becd710bc",
"username": "2cx9pyCMyn",
"active": true,
"approved": true
},
"headers": {
"common": {
"Accept": "application/json, text/plain, */*"
},
"delete": {},
"get": {},
"head": {},
"post": {
"Content-Type": "application/x-www-form-urlencoded"
},
"put": {
"Content-Type": "application/x-www-form-urlencoded"
},
"patch": {
"Content-Type": "application/x-www-form-urlencoded"
},
"content-type": "multipart/form-data"
},
"baseURL": "https://www.tuntry.com/",
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 60000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1
}
API 사용자 이름과 키를 두 번 확인했는데, 정확합니다.
Axios로 POST 요청을 감쌌을 뿐입니다.
const post = (url, data) => service.post(url, {
api_key: Config.discourse.api_key,
api_username: Config.discourse.api_username,
...data,
}
)
david
(David Taylor)
12월 3, 2019, 10:35오전
4
node에 대해 많이 알고 있지는 않지만, 제가 보기엔 괜찮아 보입니다.
시도해 볼 수 있는 한 가지 방법은 API 자격 증명을 파라미터 대신 HTTP 헤더로 전송하는 것입니다. 파라미터로 전송하는 방식은 비추천(deprecated) 상태이며, 관리자 패널에 경고가 표시됩니다. 헤더 이름은 Api-Key와 Api-Username입니다 (밑줄이 아닌 하이픈을 사용합니다). 자세한 내용은 여기 에서 확인하실 수 있습니다.
정말 대단하시네요, 네! 헤더에 Api-Key와 Api-Username을 추가하고 'content-type': 'application/json',을 추가한 후에 문제를 해결했습니다!