I'm working on an application in React Native with simple Wordpress backend using REST API and I didn't have any problems until I used GET
method.
Now I need to create a blog post using the POST
method and I'm still getting an error...
What I have:
- Wordpress with activated Basic Authentication handler plugin -> https://github.com/WP-API/Basic-Auth
- React Native project with WPAPI Client -> https://github.com/WP-API/node-wpapi
My React code:
const WPAPI = require( 'wpapi' );
let apiPromise = WPAPI.discover( 'https://myweb.com/wp-json' ).then(function( site ) {
return site.auth({
username: 'admin',
password: 'password'
});
});
apiPromise.then(function( site ) {
site.posts().create({
title: 'Your Post Title',
content: 'Your post content',
status: 'publish'
}).then(function( response ) {
console.log( response );
})
.catch(function(err){
console.log(err);
});
})
and always get this answer:
"code": "rest_cannot_create",
"status": 401,
"message": "Sorry, you are not allowed to create posts as this user."