Test ETags and conditional requests: 304 Not Modified and 412 Precondition Failed

An ETag is a fingerprint of a resource's current version. Clients send it back in two ways: If-None-Match on a read ("only send it if it changed") and If-Match on a write ("only apply this if nobody changed it since I read it"). The first saves bandwidth. The second prevents two people from silently overwriting each other's edits. Both are easy to half-implement, so they're worth a test.

1. Read and keep the ETag

HTTP
GET {{baseUrl}}/articles/{{articleId}}
JavaScript
const etag = pm.response.headers.get('ETag');
pm.test('has an ETag', () => pm.expect(etag).to.be.a('string').and.not.empty);
pm.environment.set('etag', etag);

ETags are quoted strings, like "a1b2c3", or weak ones, like W/"a1b2c3". Send them back exactly as received, quotes included.

2. Read again: 304

HTTP
GET {{baseUrl}}/articles/{{articleId}}
If-None-Match: {{etag}}
JavaScript
pm.test('not modified', () => pm.response.to.have.status(304));
pm.test('no body', () => pm.expect(pm.response.text()).to.equal(''));
pm.test('same ETag', () => pm.expect(pm.response.headers.get('ETag')).to.equal(pm.environment.get('etag')));

A server that answers 200 here works, but wastes every client's bandwidth. One that answers 304 with a body breaks the HTTP spec, and some clients.

3. Update with the right version

HTTP
PATCH {{baseUrl}}/articles/{{articleId}}
If-Match: {{etag}}
Content-Type: application/json

{ "title": "Updated title" }
JavaScript
pm.test('updated', () => pm.expect(pm.response.code).to.be.oneOf([200, 204]));
const next = pm.response.headers.get('ETag');
pm.test('new ETag', () => pm.expect(next).to.not.equal(pm.environment.get('etag')));
pm.environment.set('oldEtag', pm.environment.get('etag'));
pm.environment.set('etag', next);

4. Update with a stale version: 412

This is the one that matters. Someone else updated the article; your copy is stale:

HTTP
PATCH {{baseUrl}}/articles/{{articleId}}
If-Match: {{oldEtag}}
Content-Type: application/json

{ "title": "My stale edit" }
JavaScript
pm.test('stale write refused', () => pm.response.to.have.status(412));

Then read the article and check the title is still Updated title. A 412 that wrote anyway is worse than no check at all.

5. Edge cases worth one request each

RequestExpected
If-None-Match: * on a GET of an existing resource304
If-Match: * on a PUT to a resource that doesn't exist412
If-None-Match: * on a PUT that creates201; again → 412
A write with no If-Match, when your API requires it428 Precondition Required
Two different representations (Accept: application/json vs text/csv)different ETags

Don't forget Last-Modified

Some APIs use Last-Modified and If-Modified-Since / If-Unmodified-Since instead. The tests are the same with dates. Its one-second precision means two quick updates can share a timestamp, which is why ETags are preferred for writes.