arcyfelix

arcyfelix

Phoenix Update Behavior from Gen.JSON

Hi,
I am quite new to Phoenix and Elixir, but I love it already.
I just want to make sure the behavior I have observed is correct.
I use Phoenix 1.4.

I want to create a REST server.

  1. I created an app and installed all dependencies:
mix phx.new my-app --app my_app --no-html --no-webpack
  1. Created the DB:
cd my-app
mix ecto.create
  1. Generated a Product Schema
mix phx.gen.context Operations Product products product_name:string in_in_stock:boolean quantity:integer
  1. Then, migration:
mix ecto.migrate
  1. I generate a JSON CRUD with --no-context and --no-schema, since we already have them available:
mix phx.gen.json Operations Product products product_name:string in_in_stock:boolean quantity:integer --no-context --no-schema
  1. I add recommended resource endpoints to the router in the /api scope:
resources "/products", ProductController
  1. Then, I start the server in the dev mode with the local DB:
mix phx.server
  1. Finally, I can create the creation endpoint (by default all parameters are required):
    a) all parameters supplied - SUCCESS!
curl -H "Content-Type: application/json" -X POST -d '{"product":{"product_name":"Product1","in_in_stock":true, "quantity": 1}}' http://localhost:4000/api/products

b) missing one required parameter - FAILURE! (Correct behavior!)

curl -H "Content-Type: application/json" -X POST -d '{"product":{"product_name":"Product1","in_in_stock":true, "quantity1": 1}}' http://localhost:4000/api/products
  1. Here is when I am a little puzzled: PUT. I send a PUT request where none of the parameters in the body are correct and I get status 200OK, but the product does not change at all.
curl -H "Content-Type: application/json" -X PUT -d '{"product":{"product_name1":"Product1","in_in_stock1":true, "quantity1": 1}}' http://localhost:4000/api/products/1

Shouldn’t that return an error?
I tried changing Repo.update() to Repo.update!() in my_app/operations/operations.ex but it fails even with the correct data now.
How can I make sure that atleast one parameter (if not all that are supplied) are correct? How could I enforce all required parameters being passed in the PUT request?
What is the best practise?

Regards
Wojciech

Marked As Solved

kokolegorille

kokolegorille

You will need to make your custom validation in that case, because it’s not the default behaviour…

Those 2 points are valid in the Phoenix way, because, when updating, You rely on existing data. And it is also the case in Rails, and probably more frameworks…

Also Liked

kokolegorille

kokolegorille

To force product_name presence, You should add a validation to your changeset.

And there should be no redirection with json api. Instead, you should render a json error.

kokolegorille

kokolegorille

Because the keys You use are not in the changeset, it will cast nothing, and change nothing. Cast will simply discard wrong keys.

Try to change

"in_in_stock1":true

to

"in_in_stock":"WRONG DATA"

then it should fail, I think… or provide values that does not pass validation.

or simply provide new values to right keys, and see if it changes the record :slight_smile:

Providing wrong keys should result in a successful update, because there is nothing to change, this might be why You have a 200 status.

kokolegorille

kokolegorille

You should use cast, it’s just expected that wrong keys are silently discarded.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement