Phoenix FrameworkElixir
Last updated at 2023-10-25

PUT Endpoint for REST API in Phoenix Elixir using JSON

ClickUp
Note
AI Status
Full
Last Edit By
Last edited time
Oct 25, 2023 07:49 AM
Metatag
Slug
put-endpoint-elixir-phoenix
Writer
Published
Published
Date
Oct 25, 2023
Category
Phoenix Framework
Elixir
In this article, we will explore how to implement a PUT endpoint in a Phoenix Elixir application for a RESTful API.
The PUT request is used to update an existing resource in the API.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of Phoenix Elixir and REST API concepts.
Additionally, make sure you have the following tools installed:

Setting up the Project

First, let's create a new Phoenix project by running the following command:
mix phx.new my_api
This command will generate a new Phoenix project named my_api. Change into the project directory:
cd my_api
Next, we need to generate a new resource for our API. In this example, let's create a users resource:
mix phx.gen.json Accounts User users name:string age:integer
This command will generate the necessary files for the users resource, including the migration file, schema, and controller.

Implementing the PUT Endpoint

Open the generated lib/my_api_web/controllers/user_controller.ex file. Inside the UserController module, locate the update function.
This function handles the PUT request for updating a user.
def update(conn, %{"id" => id, "user" => user_params}) do user = Accounts.get_user!(id) changeset = Accounts.change_user(user, user_params) case Accounts.update_user(changeset) do {:ok, user} -> render(conn, "show.json", user: user) {:error, changeset} -> render(conn, "error.json", changeset: changeset) end end
In this code, we retrieve the user by its id from the Accounts module.
We then create a changeset using the change_user function, passing in the user_params received from the request.
Finally, we update the user using the update_user function from the Accounts module.

Testing the PUT Endpoint

To test the PUT endpoint, we can use Postman or any other API testing tool.
  1. Start the Phoenix server by running mix phx.server in the project directory.
  1. Open Postman and create a new request. Set the request URL to http://localhost:4000/api/users/:id, replacing :id with the ID of the user you want to update.
  1. Set the request method to PUT.
  1. In the request body, select the raw option and set the data type to JSON (application/json). Provide the updated user data in the following format:
{ "user": { "name": "John Doe", "age": 30 } }
  1. Click the Send button to send the PUT request.

Conclusion

In this article, we learned how to implement a PUT endpoint in a Phoenix Elixir application for a RESTful API.
We explored the necessary steps to set up the project, implemented the PUT request handling in the controller, and tested the endpoint using Postman.
You can now use this knowledge to update resources in your own Phoenix Elixir API.

References

Discussion (0)

Related Posts