5

I'm working on an iOS app that requires the use of WP Rest API to fetch posts. Everything is working smoothly and I fetch posts by ID using this endpoint:

https://example.com/wp-json/wp/v2/posts/12345

Most of the time the API returns the post as expected, but if the article is over 1300 words long or so, it will return an empty response. By empty I mean the page will be completely blank, not even a 404 status code or an empty array []. It does return code 200 which supposedly means everything looks good, but the response is empty.

From my troubleshooting it seems that the reason for the empty response is because the post has too many words. Is there any way for me to update the WP REST API settings? Or do I have to create a new custom endpoint that can handle the retrieval of large posts?

Any help would be much appreciated.

Update: I installed the REST API Log plugin to see what's going on. To my surprise, I am getting the correct response in the response body, but for some reason it is not showing up in any browser. Will have to investigate further.

Update2: Still nothing. I have a second wordpress installation and I decided to try and replicate things there. I imported the 1300+ word long post into the new site and the same thing happened. If you try to fetch the post by id or slug it will just return a blank page...

Small rant. Not sure what the issue is, but WP REST API has been giving me nothing but problems. Not sure why they even decided to update the API as v1 worked perfectly fine for me. v2 just gives me problem after problem.

Update3: Decided to write a custom endpoint and see what's going on. Wrote a function to only return the post_content which seems to be the issue. This is the code I got:

 function get_long_post ( $params ){
    $post = get_post(59101);

    if( empty( $post ) ){
        return null;
    }
    return $post->post_content;
 }  
 // Register the rest route here.

 add_action( 'rest_api_init', function () {
        register_rest_route( 'custom/v1', 'long-post',array(
            'methods'  => 'GET',
            'callback' => 'get_long_post'
        ) ); 
 } );

It simply pulls a post by it's ID and returns the content. I hardcoded the ID of the long post in question and surprise surprise, the response is blank. Works flawlessly for any other post that isn't super long.

Update4: This "question" might end up being super long with all my updates. But for the poor soul that has to deal with this I think documenting as much as I can is best.

I do feel I'm getting somewhere because I am now able to at least see part of the long post_content if I get a substring of the overall post_content. It seems that any substring with more than 7603 characters will NOT show in chrome or other browsers. Here is the modified function that returns at least part of the content:

     function get_long_post ( $params ){
        $post = get_post(59101);

        if( empty( $post ) ){
            return null;
        }
        //echo "<script>console.log( 'Debug Objects: " . $post->post_title . "' );</script>";
        $output =  apply_filters( 'the_content', $post->post_content );

        return substr($output,0,7603);
}

Don't ask me why it's 7603 characters. I have no clue why it's such a weird number. Going to have to investigate further.

Update5: Progress has been made! If I use echo instead of return then I get the full post content. At this point, I'm thinking of just rewriting the get REST api v2 functions I need using echo instead of return. Will have to do a bunch of formatting to get it into a valid JSON format but at this point it seems like the only solution. Will update this when I get the full functions written. After all, for my iOS app I only need a few things from the post like the post title, author, content, categories, featured image, post ID, and tag. Here is the function so far:

 function get_long_post ( $params ){
    $post = get_post(59101);

    if( empty( $post ) ){
        //return null;
    }
    $output =  apply_filters( 'the_content', $post->post_content );
    echo $output; 
    return null;
 }

1 Answer 1

0

I have the same issue as you. I figured out the problem is actually with json_encode.

I fized this by making a custom API, and converting my data to JSON using json_encode by using the options:

$json = json_encode($your_array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)

return $json

Using that solution, you no longer need to echo the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.