Wired thing happend when create user via API

When I test API users.json , I use postman to create User, it works, and activated by default, means
active=true works

But when I create a user with code in node.js. same params, active=true don’t work anymore, But I do get a success message… says,

success: true,
active: false,

how come. I don’t even see it at backend.

1 Like

My guess is that your node.js code is not passing the API key correctly, so you are not being allowed to override the active parameter. If you can share a snippet of your code here we might be able to help out.

2 Likes

Hi David, Thank you:

here is my request:

{
 "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
}

I have double-checked the API user and key, it’s correct.

I just wrapped a post request with Axios.

   const post = (url, data) => service.post(url, {
   api_key: Config.discourse.api_key,
   api_username: Config.discourse.api_username,
  ...data,
 }
)

I don’t know much about node, but that looks ok as far as I can see.

One thing you could try is sending the API credentials via HTTP headers, rather than parameters. Sending via parameters is deprecated, and will cause a warning in your admin panel. The header names are Api-Key and Api-Username (note hyphens, not underscores). More info here.

6 Likes

Great man, Yes I solved it after add Api-Key and Api-Username and add 'content-type': 'application/json', to the header!

3 Likes