Optional HTTP Headers: Expected Version
When you write to a stream you often want to use
Expected Version to allow for optimistic concurrency with a stream. You commonly use this for a domain object projection.
i.e., "A write can succeed if I have seen everyone else's writes."
ExpectedVersion with the syntax
ES-ExpectedVersion: #, where
# is an integer version number. There are other special values available:
0, the stream should exist but be empty when writing.
-1, the stream should not exist when writing.
-2, the write should not conflict with anything and should always succeed.
-4, the stream or a metadata stream should exist when writing.
ExpectedVersion does not match the version of the stream, Event Store returns an HTTP 400
Wrong expected EventNumber response. This response contains the current version of the stream in an
In the following cURL command
ExpectedVersion is not set, and it appends or create/append to the stream.
curl -i -d "@event.json" "http://127.0.0.1:2113/streams/newstream" -H "Content-Type:application/json" -H "ES-EventType: SomeEvent" -H "ES-EventId: C322E299-CB73-4B47-97C5-5054F920746E"
The stream 'newstream' has one event. If you append with an expected version of '3', you receive an error.
curl -i -d @event-version.json "http://127.0.0.1:2113/streams/newstream" -H "Content-Type:application/vnd.eventstore.events+json" -H "ES-ExpectedVersion: 3"
You can see from the
ES-CurrentVersion header above that the stream is at version 0. Appending with an expected version of 0 works. The expected version is always the version of the last event known in the stream.