How to use Openjet API?¶
In some cases you may be needing to manipulate the resources of your application via its API. This guide aims to introduce you to the world of Openjet API. For more sophisticated examples and cases follow the API Guide.
Run your application on a built-in server:
$ php bin/console server:start localhost:8000
Creating a new resource instance via API¶
Use the access_token in the request that will create a new resource.
$ curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" -d '{"attribute1": "value1", "attribute2": "value2"}' http://localhost:8000/api/{pluralized-and-urlized-resource-name}/
Tip
Read more about authorizing in API here.
Viewing a single resource instance via API¶
If you would like to show details of a resource use this command with object’s id as {id).
Remember to use the authorization token.
Assuming that you have created a resource in the previous step, it will have id = 1.
Note
Id 1 is more simple for example. Don’t forgot Openjet application use sha1 for almost all its identifiers.
$ curl -i -H "Authorization: Bearer TOKEN" http://localhost:8000/api/{pluralized-and-urlized-resource-name}/{id}
Viewing an index of resource via API¶
If you would like to see a list of all instances of your resource use such a command (provide the authorization token!):
$ curl -i -H "Authorization: Bearer TOKEN" http://localhost:8000/api/{pluralized-and-urlized-resource-name}/
Updating a single resource instance via API¶
If you would like to modify the whole existing resource use such a command (with a valid authorization token of course):
$ curl -i -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" -d '{"attribute1": "value3", "attribute2": "value4"}' http://localhost:8000/api/{pluralized-and-urlized-resource-name}/1
Partially updating a single resource instance via API¶
If you would like to update just one field of a resource use the PATCH method with such a command:
$ curl -i -X PATCH -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" -d '{"attribute1": "value5"}' http://localhost:8000/api/{pluralized-and-urlized-resource-name}/1
Deleting a single resource instance via API¶
To delete a resource instance you need to call such a command (with an authorization token):
$ curl -i -X DELETE -H "Authorization: Bearer TOKEN" http://localhost:8000/{pluralized-and-urlized-resource-name}/1