Simple Rocket Web Framework Tutorial | POST Request

rocket web framework tutorial, rocket app, rocket tutorial

Hey, I see that you are back for more learning. If you do not know why I am greeting you passionately, then you probably have not read the first part of the Rocket web framework tutorial. You can read it right here. If you have the basic grasps of the Rocket framework, then this rocket tutorial will be helpful. In the previous article, we made a rocket app; we learned how to handle a GET request and how to return an appropriate response; we also learned how we can create dynamic parameters in our URL. The user would enter a number in the URL, and then a new number would be returned that had 100 added to the original number.

If you think about it, this is not really efficient. To get or send data, GET requests are not that effective. Yeah, we can send data through a GET request, but when you have a broad scale application, sending essential data via the GET request is dangerous. In our Rocket web framework tutorial, we are not doing that much, but to do so much, we need to learn that much, right? In this rocket tutorial, I will teach you how to make the POST request and how to process that POST request in the Rocket framework.


In this Rocket web framework tutorial, we will work on the same rocket app that we made earlier, we will be making a route handler that will return an Html document. In that Html document, we will have a very simple Form. That Form will have an input field and a send button. The user will enter any number and send it. The data will be sent using the POST request. Then it will be processed similarly as the previous rocket tutorial. The user will be taken to a new route that will display a number that has 100 added to the original number.

For example, if the user inputs “5” into the input field and clicks send. “105” will be displayed on the new page. This is the basic functionality of our rocket app.


If you have read the first article or if you have some knowledge of the Rocket framework, then you will not have any significant issues. One thing to keep in mind is that we will be using the previous code of the Rocket app that we made in the last rocket tutorial. We will be adding new functionality, and it will be much easier for you to compare between the GET and POST requests by looking at one code.


In order to use Rocket and follow this rocket web framework tutorial, you need to have the Rust’s nightly version installed; you can do this by simply typing.

rocket web framework tutorial, rocket app, rocket tutorial

Make sure that after you change to the nightly version, you create a new project. If you have been following along, you can just leave this part because you know all the usual stuff :). For the people who are new, you can read the starting of the previous rocket web framework tutorial in order to set up.


This is the code that we will be adding new stuff to. I added this just so you do not have to move back and forth between this and the previous article.

rocket web framework tutorial, rocket app, rocket tutorial

In this code, the 4th line and the 17-18th line will make much more sense now. In the previous rocket web framework tutorial, these lines were not necessary, but now, as we are adding more to our rocket app. we will use these lines now.

Making the Form route

For the user to get an Html document that has a form in it, we need to make a new route. The user will go to that URL, an Html document will be returned to them that has a form that can send data via the POST request. We will add a route handler to process this request. If you have read the previous rocket web framework tutorial, then this will be an easy task.rocket web framework tutorial, rocket app, rocket tutorial

Users will be able to access this route using the link on the home page, or they can simply just type the route in the URL, and they will get the Html document. As you can see, and as I have said multiple times. There will be a simple form using which the user can send data. The Form has the method “POST,” and it is sending data to the route named “/addUsingPost,” we will take this route in the next section of this rocket web framework tutorial.

Making a route to handle POST requests

In order to process requests that are being sent from the “/form” route, we need to have another route handler that can take the data. The code is straightforward

rocket web framework tutorial, rocket app, rocket tutorial

The #[post("/addUsingPost", data="<form>")] part tells that the request has to be an HTTP POST request. In the Rocket framework, when we make a POST request to a request handler, we need another argument in the route attribute. As you can see above, it is called “data,” and it takes data.

In the route handler, we also define a parameter called data that has a type of Form, this data type is defined in the Rocket framework. The Form data type takes a Rust struct. In our rocket web framework tutorial, we need one Struct. The reason for adding a Struct in our rocket app mentioned below.

Why do we need a Struct?

If you have worked in a web framework, like Flask. Then you do not have to define a data type in the function signature. Flask takes care of the parsing and everything. As we are working in Rust, in our rocket web framework tutorial, we need to tell the Rocket app, what data type we will use to parse the Html Form. We will make a custom data type called UserForm and pass it into the Form that is available to us in Rocket.

Defining the Struct

rocket web framework tutorial, rocket app, rocket tutorial

As the data coming from the Html form has only one input field, that is why ur struct in this rocket tutorial also has one field called number. Keep in mind that the fields of the Struct have to have the same name like the names of the input fields in the Html form. We also need to implement a trait call FromForm on the Struct too.

Now, all we have to do in this rocket web framework tutorial is that we have to mount these request handlers.

Mounting the new Handlers

As we are following the previous rocket tutorial, we already have two handlers mounted, we will just add new ones like this.

rocket web framework tutorial, rocket app, rocket tutorial

Now, if you start the server and go to the root path, you will see an anchor tag that you can click to go to the “/form” path, or you can also type in the address bar of your browser. You will see an input field in which you can type any number. After entering, press the send button, and then you will see that the original number has 100 added to it.

Rust Books

The Rust Programming Language

rocket web framework tutorial, rocket app, rocket tutorial

It is not an unknown fact that you can learn a lot from a book. This official Rust book by Steve Klabnik and Carol Nichols is best for someone totally new to Rust language. It is perfect for beginners.

Check Price On Amazon

Hands-On Microservices with Rust

rocket web framework tutorial, rocket app, rocket tutorial

This is also a great book that explains everything you need to learn about data structures and algorithms with Rust.

Check Price On Amazon 


What we learned in this rocket web framework tutorial is how to handle a POST request of a user. After handling the request, now you also know how to process this request and make use of it. This rocket tutorial was straightforward and short. I am sure after making this rocket app, you have an excellent idea of POST requests in the Rocket framework. All of the processes were extremely easy to understand.

If you found this rocket web framework tutorial useful and if you learned something new, then I will love it if you add some additional features of your own in this rocket app. If this rocket tutorial was simple and easy, then you can share it with other people too. You can also take a look at Rocket’s documentation. Happy learning.

If you liked this article, you could also check out.

Leave a Reply

Your email address will not be published. Required fields are marked *