Skip to content

Sending Requests

Arun Prakash edited this page Mar 17, 2024 · 2 revisions

πŸ“© Sending API Requests

Interact with the WordPress REST API by sending requests. For example, to fetch the latest 20 posts in ascending order:

final request = ListPostRequest(
  page: 1,
  perPage: 20,
  order: Order.asc,
);

final wpResponse = await client.posts.list(request);

// Handle response
final result = wpResponse.map(
    onSuccess: (response) {
      print(response.message);
      return response.data;
    },
    onFailure: (response) {
      print(response.error.toString());
      return <Post>[];
    },
);

πŸ” WordpressResponse Insights

Utilize the WordpressResponse class to access metadata related to the response, such as request duration and status codes. Differentiate between success and failure responses to handle data appropriately.