Handling PUT Request in AdonisJs | Part 1

In this article, we are going to see how to handle PUT request in AdonisJs?
Submitted by Radib Kar, on February 12, 2021

Here, I would mainly describe the store() method already presented in your controller if you have followed our tutorial series.

So, I am assuming you already know how to send POST request & how to route that? If you don't know then please take some time to learn that from our past article.

To grab the request body, we have a method,

request.post();

You can construct an ES6 object from this like below:

{field1, field2, .. , fieldn} = request.post();

For example,

When we are sending POST request from our project collection to create a new project,

We can collect the request body like this,

const {title, description} = request.post();

This grabs all the input you send via this body.

for example, if your body is like below(in JSON):

{
    "key_a": "data_a",
    "key_b": "data_b",
    "key_c": "data_c"
}

Then the ES6 onject will be,

{key_a,. key_b, key_c} and this will extract corresponsing values from the request body(request.post()).

You can print the values in the console to check whether you can collect the request body or not.

In the ProjectController,

async store ({ request, response }) {
	const {title, description} = request.post();
	console.log(title)
	console.log(description)
	
	response.status(200).send({
		message: 'store method is handling incoming request'
	});
}

Request sent from Postman

handling PUT request (1)

Console output

handling PUT request (2)

As an assignment of this tutorial, I would like to recommend you to create the store method till now what we have learnt in the task controller too.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.