Skip to content

Commit

Permalink
Update @api-fetch README.md with query args (#49318)
Browse files Browse the repository at this point in the history
* Update @api-fetch README.md with query args

Additionally, explain that `data` is NOT included in a `GET` request.

* Separate api-fetch usage examples into different code blocks
  • Loading branch information
helgatheviking committed Mar 28, 2023
1 parent c7fc2d5 commit 113e858
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/api-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ _This package assumes that your code will run in an **ES2015+** environment. If

## Usage

### GET
```js
import apiFetch from '@wordpress/api-fetch';

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

### GET with Query Args
```js
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

const queryParams = { include: [1,2,3] }; // Return posts with ID = 1,2,3.

// POST
apiFetch( { path: addQueryArgs( '/wp/v2/posts', queryParams } ).then( ( posts ) => {
console.log( posts );
} );
```
### POST
```js
apiFetch( {
path: '/wp/v2/posts/1',
method: 'POST',
Expand Down Expand Up @@ -52,7 +66,7 @@ Unlike `fetch`, the `Promise` return value of `apiFetch` will resolve to the par
#### `data` (`object`)
Shorthand to be used in place of `body`, accepts an object value to be stringified to JSON.
Sent on `POST` or `PUT` requests only. Shorthand to be used in place of `body`, accepts an object value to be stringified to JSON.
### Aborting a request
Expand Down

1 comment on commit 113e858

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 113e858.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4542337219
📝 Reported issues:

Please sign in to comment.