Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to have response headers passed? #13

Open
te-online opened this issue Aug 9, 2017 · 6 comments
Open

Is there a way to have response headers passed? #13

te-online opened this issue Aug 9, 2017 · 6 comments

Comments

@te-online
Copy link

Thank you for this helpful library, the only one for accessing the WP REST API with PHP – as far as I can see!

Now, my goal is to load every available post from the REST API to display them in a frontend.

As of the current WordPress version, we can only get 100 posts per request from the API.

$client->posts()->get(
   null, 
   array(
      'per_page' => 100,
   )
);

Fortunately, there is a header X-WP-TotalPages, where I can see how many requests I would need to fire to get all available posts. (see https://wordpress.stackexchange.com/questions/217490/get-post-count-in-wp-rest-api-v2-and-get-all-categories/250589#250589).

However, I don't see a way to get this header from the $client instance. Is there a way that I don't see?

Right now I do requests, until there are no posts in the response anymore. This leads to one request that I could save.

@fontebasso
Copy link

When the request is triggered the response is formatted to return an array for you from the body of the Wordpress response. There is no way to get header data through the client instance.

@te-online
Copy link
Author

Thanks for clarifying this. In my opinion there should be a straight-forward way to implement pagination to make this library useful for more than just some minor use cases.

@boboudreau
Copy link
Member

This library is open source. Feel free to submit a PR if you'd like endpoints to store response headers and provide a way to retrieve them. Since we're using an HTTP library that adheres to https://www.php-fig.org/psr/psr-7/ we certainly have access to response headers. We use them here: https://github.com/varsitynewsnetwork/wordpress-rest-api-client/blob/master/src/Endpoint/AbstractWpEndpoint.php#L46-L49

@fontebasso
Copy link

@boboudreau what do you think about returning the response object for the developer implementing this library to decide whether to get the header or not, format the body json, etc?

It seems to me that it can make it difficult to implement it to have to fetch the content and format it. Something the library does now. Maybe we can bring the return into an array with the 'headers' and 'content' keys?

@te-online
Copy link
Author

what do you think about returning the response object for the developer implementing this library to decide whether to get the header or not, format the body json, etc?

I like the idea, but it would inherently break the current usage/API, which makes me concerned about backwards compatibility.

We could add another method like getWithHeaders which returns something like array('json' => 'json response ...', 'headers' => [...]), but I feel that this is kind of ugly and not the best way. Any ideas, @boboudreau? As you said, I could try to prepare a pull-request.

@mysyfy
Copy link

mysyfy commented Jun 21, 2019

+1 for getResponse / getWithHeaders to use WP header fields:

https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

  1. X-WP-Total: the total number of records in the collection
  2. X-WP-TotalPages: the total number of pages encompassing all available records

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants