• Hi WP API team,
    I need help connected with posting. I am trying to create new post, but getting error. Here is URL that I am sending POST request. I am using Basic authorization for authorization.
    https://myblog.digitalpome.com/wp-json/wp/v2/posts

    Sending this data
    {“status”:”publish”,”title”:”PageTesto”,”content”:”Standart Content”}

    Getting this error
    {
    “code”: “rest_cannot_create”,
    “message”: “Sorry, you are not allowed to create new posts.”,
    “data”: {
    “status”: 401
    }
    }

    https://wordpress.org/plugins/rest-api/

Viewing 15 replies - 1 through 15 (of 20 total)
  • Thread Starter Goharika

    (@goharika)

    Hi does anybody have such problem too?
    I have find some of forums that they are using oAuth for authentication,
    or some of them say to logout from wordpress dashboard then try to authorize with app, but that doesn’t help.

    Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    If you’re seeing a 401 return status, it means your basic authorization isn’t working and the request is being treated as a logged-out request.

    Thread Starter Goharika

    (@goharika)

    May it come because of https? because before getting ssl that works

    Thread Starter Goharika

    (@goharika)

    I have changed Basic Auth now working with oAuth, and there is no authentication error.

    I am getting this when trying to update existing post
    {
    “code”: “empty_content”,
    “message”: “Content, title, and excerpt are empty.”,
    “data”: {
    “status”: 400
    }
    }

    I am using postman tool for testing, I am choosing raw from Body tab and puting there my json
    {
    “title”: {
    “raw”: “Hello Digital World!!”
    },
    “content”: {
    “raw”: “Hello Digital Updated World!!”
    },
    “excerpt”: {
    “raw”: “test”
    }
    }

    I’m getting similar error. Any ideas?

    Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    Are you setting the Content-Type header in the request?

    Thread Starter Goharika

    (@goharika)

    Yes we should content-type as json I have add and that’s worling

    I am having the same issue you originally had Goharika. I am using angular in the front end and I want to use basic authorization. I have enqueued the api and created the nonce:

    wp_localize_script( 'angular-core', 'RestAPI', array( 'url' => get_bloginfo('wpurl').'/wp-json/wp/v2/', 'nonce' => wp_create_nonce( 'wp_rest' ) ) );

    I then have this form in html:

    <form ng-submit="processForm()">
    
    			<!-- Title -->
    			<div id="title_field" class="">
    			  <label>Title</label>
    			  <input type="text" name="title" class="" placeholder="Name of item" ng-model="formData.title">
    
    			</div>
    
    			<!-- Submit Button -->
    			<button type="submit" class="">
    			  <span class=""></span> Submit!
    			</button>
    
    		  </form>

    And this is the processForm function in my controller:

    $scope.processForm = function() {
    			console.log(RestAPI.nonce);
    
    			$http({
    				method: 'POST',
    				url: '/wp-json/wp/v2/posts',
    				params: $scope.formData,
    				transformRequest: angular.identity,
    				headers: { 'Content-Type': 'multipart/form-data', 'X-WP-Nonce': RestAPI.nonce }
    			})
    			.success(function (result) {
    				console.log('Success!');
    			}).error(function () {
    				console.log('Fail!');
    			});
    		};

    Adding the headers portion of the submit function got me from a 401 error to a 403 error, saying the nonce is invalid. I don’t know what the deal is.

    If it matters, while logged into the admin panel, if i go to the site and try and access users/me (which i know has issues listed in the documentation) I simply get logged out and repeatedly asked to login. Maybe i am misunderstanding the nonce usage?

    Thread Starter Goharika

    (@goharika)

    I can suggest to put in headers ‘Content-Type’: ‘application/json’.
    Also I have changed my authorization part, now I am using oAuth2, generating acces token and putting that in URL

    Hey all, I have opened a question on the WordPress.Stackexchange.com site regarding this exactly.

    The documentation talks about the request header as ‘X-WP-Nonce’ and then sending along the nonce. Perhaps this is what most everyone’s issue is:

    beforeSend: function ( xhr ) {
            xhr.setRequestHeader( 'X-WP-Nonce', WP_API_Settings.nonce );
    }

    However with Postman I cannot figure out how to do that?

    Please take a look at the StackExchange question.

    prasanth.p

    (@prasanthp)

    Hey,

    I got WP plugin for Basic Authentication through WP REST API. Please find the link here

    I am using the rest api plugin (version 1) with basic auth plugin its not working error[
    {
    “code”: “json_cannot_edit”,
    “message”: “Sorry, you cannot edit this post”
    }
    ]

    any idea ???

    @benracicot Thanks, that was my issue with Angular.

    Postman shares cookies with Chrome. If you are logged into your site you may see unexpected results.

    Hey Guys,

    Been through the comments above and managed to fix my authentication issues – however I’m still getting a 400 error (see pasted code below). I am trying to send post 12 to http://autoload.dev I have been through the DB and deleted every reference to post 12.

    Array
    (
        [headers] => Array
            (
                [date] => Thu, 09 Jun 2016 11:43:49 GMT
                [server] => Apache/2.2.29 (Unix) mod_wsgi/3.5 Python/2.7.10 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zh DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.22.0
                [access-control-allow-origin] => *
                [access-control-allow-methods] => POST, GET, PUT, DELETE, OPTIONS
                [access-control-allow-headers] => Array
                    (
                        [0] => X-Requested-With, content-type
                        [1] => Authorization
                    )
    
                [x-powered-by] => PHP/5.6.10
                [set-cookie] => PHPSESSID=3d20b969e225f5168b258a6fe94609ad; path=/
                [expires] => Wed, 11 Jan 1984 05:00:00 GMT
                [cache-control] => no-cache, must-revalidate, max-age=0
                [pragma] => no-cache
                [x-content-type-options] => nosniff
                [access-control-expose-headers] => X-WP-Total, X-WP-TotalPages
                [last-modified] =>
                [allow] => GET, POST
                [content-length] => 90
                [connection] => close
                [content-type] => application/json; charset=UTF-8
            )
    
        [body] => {"code":"rest_post_exists","message":"Cannot create existing post.","data":{"status":400}}
        [response] => Array
            (
                [code] => 400
                [message] => Bad Request
            )
    
        [cookies] => Array
            (
                [0] => WP_Http_Cookie Object
                    (
                        [name] => PHPSESSID
                        [value] => 3d20b969e225f5168b258a6fe94609ad
                        [expires] =>
                        [path] => /
                        [domain] => autoload.dev
                    )
    
            )
    
        [filename] =>
    )

    Can anyone explain what's going on

    Been Googling since my earlier post and found this Git Hub WP REST API post

    At the bottom of the post I saw this

    WordPress itself doesn’t really have APIs for inserting entries with new specific IDs (except in doing imports, and that’s only a “suggestion” and shouldn’t be used here), so I’d think that the REST version certainly would not either.

    Does that mean that I cannot create a post on another site with a specific ID? I am trying to json_encode a specific post from one site and post it through to another using wp_remote_post

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘WP API can't create a post’ is closed to new replies.