Skip to content

Instantly share code, notes, and snippets.

@ddamko
Last active August 29, 2015 14:18
Show Gist options
  • Save ddamko/6d3d6c214ea8ba0ff060 to your computer and use it in GitHub Desktop.
Save ddamko/6d3d6c214ea8ba0ff060 to your computer and use it in GitHub Desktop.
Testing sending events to EventStore with Elixir
iex(1)> event = [%{"eventId" => "fbf4a1a1-b4a3-4dfe-a01f-ec52c34e16e4", "eventType" => "event-type", "data" => %{"a" => "1"}}]
[%{"data" => %{"a" => "1"}, "eventId" => "fbf4a1a1-b4a3-4dfe-a01f-ec52c34e16e4",
"eventType" => "event-type"}]
iex(2)> {:ok, json_event} = JSX.encode event
{:ok,
"[{\"data\":{\"a\":\"1\"},\"eventId\":\"fbf4a1a1-b4a3-4dfe-a01f-ec52c34e16e4\",\"eventType\":\"event-type\"}]"}
iex(3)> {:ok, post} = HTTPoison.request(:post, "http://127.0.0.1:2113/streams/newstream", json_event, [{"Content-Type", "vnd.eventstore.events+json"}])
{:ok,
%HTTPoison.Response{body: "",
headers: %{"Access-Control-Allow-Headers" => "Content-Type, X-Requested-With, X-PINGOTHER, Authorization, ES-LongPoll, ES-ExpectedVersion, ES-EventId, ES-EventType, ES-RequiresMaster, ES-HardDelete, ES-ResolveLinkTo, ES-ExpectedVersion",
"Access-Control-Allow-Methods" => "", "Access-Control-Allow-Origin" => "*",
"Access-Control-Expose-Headers" => "Location, ES-Position",
"Connection" => "close", "Content-Length" => "0", "Content-Type" => "",
"Date" => "Thu, 02 Apr 2015 15:33:39 GMT", "Server" => "Mono-HTTPAPI/1.0"},
status_code: 500}}
EventStore Message: Unhandled exception while processing http request at [http://127.0.0.1:2113/, http://localhost:2113/].
componentText
## FIX ##
defmodule EventStore do
def store_event(stream, event_type, event_data) do
{:ok, data} = JSX.encode event_data
HTTPoison.request(:post, "http://localhost:2113/streams/#{stream}", data, [{"Content-Type","application/json"},{"ES-EventType",event_type},{"ES-EventId",UUID.uuid1}])
end
end
@gregoryyoung
Copy link

trying to leave comments doesn't work!

@gregoryyoung
Copy link

oh now it does it was 404'ing.

  1. what else is in the log .... e.g. around that.

  2. You should be able to reproduce this in curl rather trivially?

@ddamko
Copy link
Author

ddamko commented Apr 2, 2015

I was able to solve my issue by posting the EventType and EventId in the headers rather then in the JSON data.

@gregoryyoung
Copy link

then you should change your content type to application/json

http://docs.geteventstore.com/http-api/3.1.0-pre/writing-to-a-stream/

@gregoryyoung
Copy link

btw your content type should be application/vnd.eventstore.events+json in your original example ...

@ddamko
Copy link
Author

ddamko commented Apr 2, 2015

Ah yes it should. Thanks. I am going to be working this into a CQRS example I am playing with in Elixir. https://github.com/ddamko/elixir_cqrs_es

@ddamko
Copy link
Author

ddamko commented Apr 2, 2015

I do continually get this error after running ./run-node.sh on my linux machine

Error while reading drive info for path /tmp/EventStore/2015-04-02_21.50.30.673538-Node2113. Message: The drive name does not exist
Parameter name: driveName.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment