Request to REST endpoint works fine in browser and curl, but fails from WP_REST_Request












1















I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.



Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:



functions.php



add_action('wp_head', 'write_json_to_script_tag');
function write_json_to_script_tag(){
$request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
$response = rest_do_request( $request );
var_dump($response);
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
$json = wp_json_encode( $data );
echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
}


When I hit https://example.com/wp-json/wp/v2/pages/499 in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}










share|improve this question



























    1















    I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.



    Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:



    functions.php



    add_action('wp_head', 'write_json_to_script_tag');
    function write_json_to_script_tag(){
    $request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
    $response = rest_do_request( $request );
    var_dump($response);
    $server = rest_get_server();
    $data = $server->response_to_data( $response, false );
    $json = wp_json_encode( $data );
    echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
    }


    When I hit https://example.com/wp-json/wp/v2/pages/499 in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}










    share|improve this question

























      1












      1








      1


      1






      I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.



      Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:



      functions.php



      add_action('wp_head', 'write_json_to_script_tag');
      function write_json_to_script_tag(){
      $request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
      $response = rest_do_request( $request );
      var_dump($response);
      $server = rest_get_server();
      $data = $server->response_to_data( $response, false );
      $json = wp_json_encode( $data );
      echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
      }


      When I hit https://example.com/wp-json/wp/v2/pages/499 in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}










      share|improve this question














      I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.



      Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:



      functions.php



      add_action('wp_head', 'write_json_to_script_tag');
      function write_json_to_script_tag(){
      $request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
      $response = rest_do_request( $request );
      var_dump($response);
      $server = rest_get_server();
      $data = $server->response_to_data( $response, false );
      $json = wp_json_encode( $data );
      echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
      }


      When I hit https://example.com/wp-json/wp/v2/pages/499 in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}







      rest-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 6 '18 at 16:22









      vlasitsvlasits

      1206




      1206






















          1 Answer
          1






          active

          oldest

          votes


















          3














          The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:




          • Wrong: /wp-json/wp/v2/pages/499/


          • Correct: /wp/v2/pages/499



          So:



          $request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );





          share|improve this answer
























          • My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

            – vlasits
            Dec 6 '18 at 17:04






          • 2





            It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

            – Sally CJ
            Dec 6 '18 at 17:09











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "110"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f321212%2frequest-to-rest-endpoint-works-fine-in-browser-and-curl-but-fails-from-wp-rest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:




          • Wrong: /wp-json/wp/v2/pages/499/


          • Correct: /wp/v2/pages/499



          So:



          $request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );





          share|improve this answer
























          • My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

            – vlasits
            Dec 6 '18 at 17:04






          • 2





            It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

            – Sally CJ
            Dec 6 '18 at 17:09
















          3














          The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:




          • Wrong: /wp-json/wp/v2/pages/499/


          • Correct: /wp/v2/pages/499



          So:



          $request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );





          share|improve this answer
























          • My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

            – vlasits
            Dec 6 '18 at 17:04






          • 2





            It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

            – Sally CJ
            Dec 6 '18 at 17:09














          3












          3








          3







          The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:




          • Wrong: /wp-json/wp/v2/pages/499/


          • Correct: /wp/v2/pages/499



          So:



          $request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );





          share|improve this answer













          The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:




          • Wrong: /wp-json/wp/v2/pages/499/


          • Correct: /wp/v2/pages/499



          So:



          $request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 6 '18 at 16:53









          Sally CJSally CJ

          3,9812415




          3,9812415













          • My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

            – vlasits
            Dec 6 '18 at 17:04






          • 2





            It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

            – Sally CJ
            Dec 6 '18 at 17:09



















          • My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

            – vlasits
            Dec 6 '18 at 17:04






          • 2





            It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

            – Sally CJ
            Dec 6 '18 at 17:09

















          My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

          – vlasits
          Dec 6 '18 at 17:04





          My brain just skipped over the wp-json omission in the documentation: developer.wordpress.org/reference/classes/wp_rest_request

          – vlasits
          Dec 6 '18 at 17:04




          2




          2





          It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

          – Sally CJ
          Dec 6 '18 at 17:09





          It happens sometimes.. :) And (although you may already know), you can find the list of WordPress default routes here - and in your case, the route is here under the "Definition" section - /wp/v2/pages/<id>.

          – Sally CJ
          Dec 6 '18 at 17:09


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to WordPress Development Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f321212%2frequest-to-rest-endpoint-works-fine-in-browser-and-curl-but-fails-from-wp-rest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How do I know what Microsoft account the skydrive app is syncing to?

          When does type information flow backwards in C++?

          Grease: Live!