$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WordPress REST Api: update user|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WordPress REST Api: update user

matteradmin9PV0评论

I have created an user via wordpress api, then after registration, I save user's username and password into the sessionStorage, and then, finaly, try to edit user's profile. Here's my post request data

POST /wp-json/wp/v2/users?context=edit HTTP/1.1
Host: something
Connection: keep-alive
Content-Length: 292
Accept: application/json, text/plain, */*
Origin: http://localhost:8100
Authorization: Bearer ********************************************************************************************************
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

POST Body

"postData": {
            "mimeType": "application/json;charset=UTF-8",
            "text": "{\"first_name\":\"Foo\",\"last_name\":\"Bar\",\"city\":\"Belgrade\",\"zip\":11000,\"address\":\"Foobar 7\",\"email\":\"[email protected]\",\"username\":\"foobar\",\"name\":\"Foo Bar\",\"nickname\":\"Foo Bar\",\"password\":\"myexamplepass\",\"context\":\"edit\",\"meta\":{\"city\":\"Belgrade\",\"zip\":11000,\"address\":\"FooBar 17\"}}"
          }

Now, with that request, I get {"code":"rest_cannot_create_user","message":"Sorry, you are not allowed to create new users.","data":{"status":403}}

Response is:

{
    "code": "rest_cannot_create_user",
    "message": "Sorry, you are not allowed to create new users.",
    "data": {
        "status": 403
    }
}

Now, if I try to create a post with that very user, via postman, it works properly. What am I doing wrong here?

I have created an user via wordpress api, then after registration, I save user's username and password into the sessionStorage, and then, finaly, try to edit user's profile. Here's my post request data

POST /wp-json/wp/v2/users?context=edit HTTP/1.1
Host: something
Connection: keep-alive
Content-Length: 292
Accept: application/json, text/plain, */*
Origin: http://localhost:8100
Authorization: Bearer ********************************************************************************************************
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

POST Body

"postData": {
            "mimeType": "application/json;charset=UTF-8",
            "text": "{\"first_name\":\"Foo\",\"last_name\":\"Bar\",\"city\":\"Belgrade\",\"zip\":11000,\"address\":\"Foobar 7\",\"email\":\"[email protected]\",\"username\":\"foobar\",\"name\":\"Foo Bar\",\"nickname\":\"Foo Bar\",\"password\":\"myexamplepass\",\"context\":\"edit\",\"meta\":{\"city\":\"Belgrade\",\"zip\":11000,\"address\":\"FooBar 17\"}}"
          }

Now, with that request, I get {"code":"rest_cannot_create_user","message":"Sorry, you are not allowed to create new users.","data":{"status":403}}

Response is:

{
    "code": "rest_cannot_create_user",
    "message": "Sorry, you are not allowed to create new users.",
    "data": {
        "status": 403
    }
}

Now, if I try to create a post with that very user, via postman, it works properly. What am I doing wrong here?

Share Improve this question asked May 14, 2018 at 23:55 errorouserrorous 1691 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

My REST URL was wrong - I didn't add the userid to the url. Proper REST URL should be: /wp-json/wp/v2/users/30/ where 30 should be changed with user's id.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far