Correct way to use curl for testing phoenix api

I am trying to make a basic session api using a tutorial (https://medium.com/@diamondgfx/building-a-reading-queue-in-ember-and-phoenix-part-1-getting-started-with-phoenix-521a19814ae5) but am running into a little conceptual trouble. My SessionController has a create function that takes an argument like:

def create(conn, %{"user" => user_params}) do

I thought I would be clever and test this on a valid user using curl like so:

curl -X POST http://localhost:4000/api/v1/sessions -d '{"email":"test@test.com","password":"s3cr3tp4ss"}'

but I get a non-matching error:

** (Phoenix.ActionClauseError) bad request to Picki.SessionApiController.create, no matching action clause to process request

where the relevant information seems to be:

Parameters: %{"{\"email\":\"test@test.com\",\"password\":\"s3cr3tp4ss\"}" => "[FILTERED]"}

So I used pry to take a peak and it also shows that the input looks like

%{"{\"email\":\"test@test.com\",\"password\":\"s3cr3tp4ss\"}" => nil}

I tried a bunch of different ways of structuring the curl input data, but none of them fared any better. So my question is, what is the right way to test this function? Is there a way to structure the input data from curl so that it matches? Thanks!

You aren’t passing the user param, so it never matches %{“user” => user_params}.

2 Likes

I’m sorry, I’m not very comfortable with how things should look yet. Do you mean it should be

... -d '{"user":{"email":"test@test.com","password":"s3cr3tp4ss"}}' ...

because I have tried this (and several variations), but to no avail.

You should tell curl you are sending JSON with -H "Content-Type: application/json". Otherwise it will send it as POST form data.

2 Likes

Thanks so much, it looks like that did the trick!

1 Like