Site icon Hip-Hop Website Design and Development

WP REST API returns clean response if submit is simply too lengthy

I am engaged on an iOS app that requires using WP Relaxation API to fetch posts. Every thing is working easily and I fetch posts by ID utilizing this endpoint:

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

More often than not the API returns the submit as anticipated, but when the article is over 1300 phrases lengthy or so, it is going to return an empty response. By empty I imply the web page shall be utterly clean, not even a 404 standing code or an empty array []. It does return code 200 which supposedly means all the pieces appears to be like good, however the response is empty.

From my troubleshooting it appears that evidently the rationale for the empty response is as a result of the submit has too many phrases. Is there any manner for me to replace the WP REST API settings? Or do I’ve to create a brand new customized endpoint that may deal with the retrieval of huge posts?

Any assist can be a lot appreciated.

Replace: I put in the REST API Log plugin to see what is going on on. To my shock, I get the right response within the response physique, however for some purpose it isn’t displaying up in any browser. Should examine additional.

Update2: Nonetheless nothing. I’ve a second wordpress set up and I made a decision to try to replicate issues there. I imported the 1300+ phrase lengthy submit into the brand new web site and the identical factor occurred. If you happen to attempt to fetch the submit by id or slug it is going to simply return a clean web page…

Small rant. Undecided what the problem is, however WP REST API has been giving me nothing however issues. Undecided why they even determined to replace the API as v1 labored completely high quality for me. v2 simply offers me downside after downside.

Update3: Determined to write down a customized endpoint and see what is going on on. Wrote a operate to solely return the post_content which appears to be the problem. That is the code I received:

 operate get_long_post ( $params ){
    $submit = get_post(59101);

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

 add_action( 'rest_api_init', operate () {
        register_rest_route( 'customized/v1', 'long-post',array(
            'strategies'  => 'GET',
            'callback' => 'get_long_post'
        ) ); 
 } );

It merely pulls a submit by it is ID and returns the content material. I hardcoded the ID of the lengthy submit in query and shock shock, the response is clean. Works flawlessly for another submit that is not tremendous lengthy.

Update4: This “query” would possibly find yourself being tremendous lengthy with all my updates. However for the poor soul that has to take care of this I feel documenting as a lot as I can is greatest.

I do really feel I am getting someplace as a result of I’m now in a position to at the very least see a part of the lengthy post_content if I get a substring of the general post_content. Evidently any substring with greater than 7603 characters will NOT present in chrome or different browsers. Right here is the modified operate that returns at the very least a part of the content material:

     operate get_long_post ( $params ){
        $submit = get_post(59101);

        if( empty( $submit ) ){
            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);
}

Do not ask me why it is 7603 characters. I’ve no clue why it is such a bizarre quantity. Going to have to analyze additional.

Update5: Progress has been made! If I take advantage of echo as a substitute of return then I get the total submit content material. At this level, I am considering of simply rewriting the get REST api v2 capabilities I would like utilizing echo as a substitute of return. Should do a bunch of formatting to get it into a legitimate JSON format however at this level it looks as if the one answer. Will replace this after I get the total capabilities written. In any case, for my iOS app I solely want just a few issues from the submit just like the submit title, creator, content material, classes, featured picture, submit ID, and tag. Right here is the operate up to now:

 operate get_long_post ( $params ){
    $submit = get_post(59101);

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