Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#40697 closed defect (bug) (worksforme)

Can't create new post by REST API but I can update existing post

Reported by: alaa-rihan's profile Alaa Rihan Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.4
Component: REST API Keywords:
Focuses: Cc:

Description

Hi,

I'm trying to let users create/edit/delete posts from frontend using WordPress REST API, everything working but creating new post, this is my code

jQuery( document ).ready( function ( $ ) {
    $( '.add-new-project button' ).click( function(e) {
        var title = 'test111';
        var excerpt = 'ssss';
        var content = 'cdffddfsf';
        var status = 'draft';

        var data = {
            title: title,
            excerpt: excerpt,
            content: content
        };

        $.ajax({
            method: "POST",
            url: apiData.apiURL + 'wp/v2/posts',
            data: data,
            beforeSend: function ( xhr ) {
                xhr.setRequestHeader( 'X-WP-Nonce', apiData.nonce );
            },
            success : function( response ) {
                console.log( response );
            },
            fail : function( response ) {
                console.log( response );
            }

        });

    });

} );

This the the response I get when I try to create post:

{code: "rest_no_route", message: "No route was found matching the URL and request method",…}

Change History (5)

#1 @rmccue
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

The message you're receiving is a 404, so it's likely that the URL you're trying to hit is wrong. Check that the URL you're trying to send to is correct via your browser's dev tools.

Additionally, WordPress Trac is for reporting issues, whereas this is better suited to the support forums. If you can't work out what's going wrong, ask on the forums for some help debugging. :)

#2 @Alaa Rihan
7 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

I knew you will tell me that, but I can confirm I tried to access the api url I used from browser, I get the posts as json format as it expected, so the api url is correct, I used this code to get the api url:

<?php
wp_localize_script( 'myapp', 'apiData', array(
        'nonce' => wp_create_nonce( 'wp_rest' ),
        'apiURL' => rest_url('wp/v2'),
) );

and I told you I can edit/delete posts without problems, this is WP bug, please check it yourself.

#3 @JPry
7 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

As @rmccue mentioned, the URL you're hitting is wrong. You're defining apiURL as already containing "wp/v2", yet you're trying to append it again in your script. Written as-is, the URL for the Ajax request will be something like http://eample.com/wp-json/wp/v2wp/v2/posts. This is why you're getting the 404 error. The correct URL would be http://example.com/wp-json/wp/v2/posts.

Last edited 7 years ago by JPry (previous) (diff)

#4 @Alaa Rihan
7 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

You are right about this but I fixed the url in my code, the problem still happening, this is my current code:

jQuery( document ).ready( function ( $ ) {
    $( '.add-new-project button' ).click( function(e) {
        var title = 'test111';
        var excerpt = 'ssss';
        var content = 'cdffddfsf';
        var status = 'draft';

        var data = {
            title: title,
            excerpt: excerpt,
            content: content
        };

        $.ajax({
            method: "POST",
            url: apiData.apiURL + 'posts',
            data: data,
            beforeSend: function ( xhr ) {
                xhr.setRequestHeader( 'X-WP-Nonce', apiData.nonce );
            },
            success : function( response ) {
                console.log( response );
            },
            fail : function( response ) {
                console.log( response );
            }

        });

    });

} );

my final url is like this:
https://mydomain.com/wp-json/wp/v2/posts

When I enter the url above in the browser I get posts in JSON format and when I send post request to edit existing post, the url will be like this:
https://mydomain.com/wp-json/wp/v2/posts/342

It works normally, the post updated correctly, but when I send post request to create new post to this url:
https://mydomain.com/wp-json/wp/v2/posts

I get 404 error!

#5 @JPry
7 years ago

  • Resolution set to worksforme
  • Status changed from reopened to closed

The problem is your code.

You need a / before posts, like this: url: apiData.apiURL + '/posts'

I created a new site using WordPress 4.7.4 and used the same script localization you provided:

<?php
        wp_localize_script( 'myapp', 'apiData', array(
                'nonce' => wp_create_nonce( 'wp_rest' ),
                'apiURL' => rest_url( 'wp/v2' ),
        ) );

I created very similar JS to post to the API:

jQuery.ajax({
    method: "POST",
    url: apiData.apiURL + '/posts',
    data: {
        title: "Test from the api!",
        content: "This is my cool post content."
    },
    beforeSend: function (r) {
        r.setRequestHeader('X-WP-Nonce', apiData.nonce);
    }, success: function (r) {
        console.log(r);
    }, fail: function (r) {
        console.log(r);
    }
});

I got a successful response and a new post created.

Last edited 7 years ago by JPry (previous) (diff)
Note: See TracTickets for help on using tickets.