Post Reads 14 reads

How to install package @wordpress/api-fetch? How to install package @wordpress/api-fetch?

npm install @wordpress/api-fetch --save

How to use package @wordpress/api-fetch? How to use package @wordpress/api-fetch?

Examples Examples

Example of GET request in @wordpress/api-fetch Example of GET request in @wordpress/api-fetch

import apiFetch from '@wordpress/api-fetch';

// Request with GET method.
apiFetch( { path: '/wp/v2/posts' } ).then( posts => {
	// console.log( posts );
} );

Example of POST request in @wordpress/api-fetch Example of POST request in @wordpress/api-fetch

import apiFetch from '@wordpress/api-fetch';

// Request with POST method.
apiFetch( {
	path: '/wp/v2/posts/1',
	method: 'POST',
	data: { title: 'My New Post' },
} ).then( res => {
	console.log( res );
} );

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: