Create a post with the REST API and an application password
Description: Create an application password and use it to add a draft post Posted by API through the REST API. Show the ID and the status of the new post.
Hint:
- Use Basic authentication:
curl -u user:application-password. - The method for creating is
POSTon/wp-json/wp/v2/posts.
Solution
Terminal output
$ curl -s -u "admin:APP_PASSWORD" -X POST "http://example.com/wp-json/wp/v2/posts" -H "Content-Type: application/json" -d '{"title":"Posted by API","status":"draft"}' | jq -c '{id, status, type}'
{"id":5,"status":"draft","type":"post"}
$ curl -s -X POST "http://example.com/wp-json/wp/v2/posts" -d "title=No login" | jq -c '{code, status: .data.status}'
{"code":"rest_cannot_create","status":401}
How it works
- The application password proves who you are, so WordPress accepts the request (code 201 and the new post).
- Without the login, the same request is refused with the status 401.
Read REST API Authentication.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |