question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error "rest_cannot_create", status 401

See original GitHub issue

Hi,

I’m making an app with React Native, and I use this module to handle WP API requests. I’m just messing around right now, but I keep having an error: “rest_cannot_create”, with status 401 and message saying that I don’t have permission to perform my request.

Here’s how I perform the basic auth:

const wp = new WPAPI({
  endpoint: 'https://www.my-url.com/wp-json',
  username: 'username',
  password: 'passphrase',
  auth: true
});

And my request looks like this:

wp.media()
        .file( uri )
        .create({
            title: 'My awesome image',
            alt_text: 'an image of something awesome',
            caption: 'This is the caption text',
            description: 'More explanatory information'
        })
        .then(function( response ) {
            console.log(response); 
            var newImageId = response.id;
            return wp.media().id( newImageId ).update({
                post: userId
            });
        })
        .then(function( response ) {
            dispatch(setNewImageAvatar(response));
        })
        .catch(function( err ) {
          console.log(err);
        });

And well, I used the WP Basic Auth handler and enabled it: https://github.com/WP-API/Basic-Auth

What do I miss?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14

github_iconTop GitHub Comments

5reactions
fitzmodecommented, May 5, 2017

I have the same issue , my code is as below . I get the { code: 'rest_cannot_create',message: 'Sorry, you are not allowed to create posts as this user.',data: { status: 401 } } error. How can I resolve this, and is there any extra config needed on the WP dashboard side?

var WPAPI = require('wpapi');
var wp = new WPAPI({
    endpoint: 'http://my-url.com/wp-json',
    username: 'username',
    password: ' password',
    auth: true
});


wp.posts().create({
    // "title" and "content" are the only required properties
    title: 'Your Post Title',
    content: 'Your post content',
    // Post will be created as a draft by default if a specific "status"
    // is not specified
    status: 'publish'
}).then(function(response) {
    // "response" will hold all properties of your newly-created post,
    // including the unique `id` the post was assigned on creation
    console.log(response.id);
}).catch(function(err) {
    console.log(err);
})
1reaction
kadamwhitecommented, Jan 24, 2019

If we introduce a generic way to modify outgoing requests (see #324) that will permit adding that query parameter without modifying the core library code. I will update this issue once we’ve implemented an option for that behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

401 Unauthorized - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed ...
Read more >
How to Quickly Fix the 401 Unauthorized Error (5 Methods)
The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the ...
Read more >
Error: Rest Cannot Create - WordPress.org
A 401 response status indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource....
Read more >
401 Error - Rest Cannot Create · Issue #321 · WP-API/node ...
I have a locally hosted wp install with the auth plugin installed and am using a node script to interface with the site...
Read more >
How to Fix a 401 Unauthorized Error? - GeeksforGeeks
The 401 Unauthorized Error is an HTTP status code error that represented the request sent by the client to the server that lacks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found