Ensure consistency with headers
Ensure consistency with headers
Some Box APIs support headers used to ensure consistency between your application and Box.
etag
, if-match
, and if-none-match
Many of the file system items (files or folders) that can be requested via the
API return an etag
value for the item.
For example, a file resource returns an etag
in the JSON response.
curl https://api.box.com/2.0/files/12345 \
-H "authorization: Bearer ACCESS_TOKEN"
{
"id": 12345,
"etag": 1,
"type": "file",
"sequence_id": 3,
"name": "Contract.pdf",
...
}
This etag
can be used as the value of a if-match
or if-none-match
header to either ensure a resource hasn't changed since the etag
value was
received, or to prevent unnecessary downloads for items that haven't changed.
For example, to fetch the same file only if it has changed, pass in the etag
value in a if-none-match
header.
curl https://api.box.com/2.0/files/12345 \
-H "authorization: Bearer ACCESS_TOKEN" \
-H "if-none-match: 1"
This API call would result in an empty response if the file had not changed.
Ensure consistent changes
The if-match
header allows your application to ensure that no changes are
made to items when another application or a user has made changes to the item
since your application last inspected it. This helps ensure that
changes aren't lost when two applications or users are changing items at the
same time.
The following endpoints support this header.
if-match capable endpoints | |
---|---|
POST /files/:id/content | Upload a new file version |
PUT /files/:id | Update a file's information |
DELETE /files/:id | Delete a file |
PUT /folders/:id | Update a folder's information |
DELETE /folders/:id | Delete a folder |
PUT /web_links/:id | Update a web link's information |
DELETE /web_links/:id | Delete a web link |
The response of these APIs calls depends on the existence of the item,
and whether the etag
value matches the most recent version.
Item found? | Etag match? | HTTP status |
---|---|---|
Yes | Yes | 200 |
Yes | No | 412 |
No | Yes | 412 |
No | No | 404 |
Prevent unnecessary request downloads
The if-none-match
header allows your application to ensure that no information
is downloaded for items that have not changed since your application last
inspected it. This helps ensure no unnecessary information is downloaded,
speeding up your application and saving on bandwidth.
if-none-match capable endpoints | |
---|---|
GET /files/:id | Get a file's information |
GET /folders/:id | Get a folder's information |
GET /web_links/:id | Get a web link's information |
GET /shared_items | Get a shared item's information |
The response of these APIs calls depends on the existence of the item,
and whether the etag
value matches the most recent version.
Item found? | Etag match? | HTTP Status |
---|---|---|
Yes | Yes | 304 |
Yes | No | 200 |
No | Yes | 404 |
No | No | 404 |