Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

What is put in api?

2 Answer(s) Available
Answer # 1 #

The *PUT method *(HTTP PUT request method) creates a new resource or updates (substitutes) a representation of the target resource with the request payload. This means a Put request updates a resource at a specified URI. It is also used to create a new resource at the given URI or replace the entire product entity.

The official HTTP RFC specifies:

Let us now discuss the main differences between a PUT and a POST request.

Status codes obtained for PUT and POST requests

The methods POST and PUT use the following status codes:

POST request

PUT request

Now we'll demonstrate the PUT request using Swagger UI. The URL for the PUT request is given here https://demoqa.com/BookStore/v1/Books/

Also, you can take a look at the Book Store Swagger

When we access this URL, we get the following screen.

In the above screen, we a trying to update a book record with ISBN="9781449325862". The two parameters that we will update are userId: "toolsqa_test" and ISBN: "9781449325865". When we execute this put request we get the following output.

We can see the status code 200 which we received from the server indicating the request was successful.

In the next section, we will implement the PUT request using Rest Assured.

As explained in the tutorial on a *POST request, to create JSON objects, we will add a Simple JSON* library in the classpath in the code. Once this is done, we follow the below-given steps to put a request using REST Assured.

Let us discuss each of these steps below.

We will use the same URL for implementing the PUT request using Rest Assured.

Endpoint: https://demoqa.com/BookStore/v1/Books/

The first step is to create a JSON data request that we need to send using the "put()" method. The following piece of code achieves this.

java import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; import org.junit.Test;

public class PUTMethod { @Test public void putMethod() {

}

In the above code, we have used an ISBN parameter. Note that we already have a record with this ISBN. We will update this record by sending the PUT request. So for this ISBN, we will update the record with a new ISBN and a userId. We set a new value for the ISBN and also updated userId. So when we send this request, the record having ISBN="9781449325862" will have its userId="toolsqa_test" and ISBN updated to the new value, "9781449325865".

The generated request is sent to the server.

java Response res = httpRequest.body("{ "isbn": "" + isbn + "", "userId": "" + userId + ""}")

.put("/BookStore/v1/Book/9781449325862");

So in the above code, we have created a request body as a JSON string and then we call the "put()" method with this request by sending the ISBN as an argument. This ensures the record with the given ISBN is updated.

The next step is to validate the response to ensure record updation. This is done by reading the status code of the response as follows:

java //validate the response System.out.println("The response code - " +res.getStatusCode());

Assert.assertEquals(res.getStatusCode(),200);

We have used the getStatusCode () method that returns the status code of the response. The value 200 indicates the PUT request was successful. The execution of the above tests gives the following response:

We see that the test case has passed.

The complete code for the PUT request demonstration using Rest Assured is as follows:

java package org.example;

import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; import org.junit.Assert; import org.junit.Test; import io.restassured.response.Response; import io.restassured.response.ResponseBody; import io.restassured.specification.RequestSpecification; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest;

public class PUTMethod { String userId= "toolsqa_test"; String baseUrl="https://demoqa.com"; String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyTmFtZSI6InRlc3RpbmcxMjMiLCJwYXNzd29yZCI6IlBhc3N3b3JkQDEiLCJpYXQiOjE2Mjg1NjQyMjF9.lW8JJvJF7jKebbqPiHOBGtCAus8D9Nv1BK6IoIIMJQ4"; String isbn ="9781449325865";

}

The steps discussed above demonstrate the basic working of a PUT request and how it differs from a POST request. We used the JSON library to create JSON data sent in the content of the body of the request. We then validate the response using the status code after obtaining the response.

Note: Video tutorial for Put request is available at Put Request in Rest Assured

In this article, we discussed the PUT request.

[5]
Edit
Query
Report
Tyger Shipp
Chief Data Officer
Answer # 2 #

A PUT method puts or places a file or resource precisely at a specific URI. In case a file or a resource already exists at that URI, the PUT method replaces that file or resource. If there is no file or resource, PUT creates a new one.

[0]
Edit
Query
Report
free Yumi
ACCOUNTANT SYSTEMS

Related Questions

No More Questions available at this moment!