wp-api-angular
wp-api-angular copied to clipboard
rest_cannot_create Unauthorized error when creating post
Hi, please i have this error when trying to create post : Response {_body: "{"code":"rest_cannot_create","message":"Sorry, you…reate posts as this user.","data":{"status":401}}", status: 401, ok: false, statusText: "Unauthorized", headers: Headers, …} headers : Headers {_headers: Map(1), _normalizedNames: Map(1)} ok : false status : 401 statusText : "Unauthorized" type : 2 url : "http://aqarat.estnhad.com/wp-json/wp/v2/posts" _body : "{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create posts as this user.","data":{"status":401}}" proto : Body
and this the code:
console.log(this.values.userData.token);
let headers = new Headers();
headers.append('Content-Type', 'application/json;charset=UTF-8');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Headers', 'Content-Type, Content-Range, Content-Disposition, Content-Description');
headers.append('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
headers.append('Authorization', 'Bearer ' + this.values.userData.token);
headers.append('Cache-Control', 'no-cache');
this.wpApiPosts.create({ "title": 'wpapi angu title', "status": 'publish', "content": 'wpapi angu' }, headers)
.toPromise()
.then(response => response.json())
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});