HTTP POST method is used to send data to the Server. POST method is called when you submit any form data (HTML forms) on a web page. For e.g. submission of the registration form of Yahoo for new user account creation.
To practice this please use any API on which you can perform POST operation. Suppose we have to pass the below data for new registration. If registration gets successful then we will get the status line as below which comprises of HTTP protocol version and HTTP Code and HTTP message.
HTTP/1.1 201 Created
Below request body we have to add in the Post request in JSON format. In JSON it stores the data in {key,value} pairs.
{
"FirstName","Staple"
"LastName","Solutions"
"UserName","Staple@123"
"Password","Password@23"
"Email","abc@staples.com"
}
To add the above JSON body in the POST request we have to use JSONObejct like below:
JSONObject js=new JSONObject();
To support the JSON Object we have to add the Simple JSON jar file i.e.
import org.json.simple.JSONObject;
import org.json.simple.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
public class RestPost {
public static void main(String[] args)
{
RestAssured.baseURI="Pass URI of the resource";
RequestSpecification request=RestAssured.given();
JSONObject js=new JSONObject();
js.put("FirstName","Staple");
js.put("LastName","Solutions");
js.put("UserName","Staple@123");
js.put("Password","Password@23");
js.put("Email","abc@staples.com");
request.body(js.toJSONString());
Response response=request.post("/register");
ResponseBody rbody=response.getBody();
String txtBody=rbody.asString();
System.out.println("Status line is :"+ response.statusLine());
System.out.println("Status code is :"+ response.statusCode());
System.out.println("*******************************************************");
System.out.println("Response body :");
System.out.println(txtBody);
System.out.println("*******************************************************");
System.out.println("All header information are given below:");
System.out.println(response.getHeaders());
}
}
To practice this please use any API on which you can perform POST operation. Suppose we have to pass the below data for new registration. If registration gets successful then we will get the status line as below which comprises of HTTP protocol version and HTTP Code and HTTP message.
HTTP/1.1 201 Created
Below request body we have to add in the Post request in JSON format. In JSON it stores the data in {key,value} pairs.
{
"FirstName","Staple"
"LastName","Solutions"
"UserName","Staple@123"
"Password","Password@23"
"Email","abc@staples.com"
}
To add the above JSON body in the POST request we have to use JSONObejct like below:
JSONObject js=new JSONObject();
To support the JSON Object we have to add the Simple JSON jar file i.e.
import org.json.simple.JSONObject;
import org.json.simple.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
public class RestPost {
public static void main(String[] args)
{
RestAssured.baseURI="Pass URI of the resource";
RequestSpecification request=RestAssured.given();
JSONObject js=new JSONObject();
js.put("FirstName","Staple");
js.put("LastName","Solutions");
js.put("UserName","Staple@123");
js.put("Password","Password@23");
js.put("Email","abc@staples.com");
request.body(js.toJSONString());
Response response=request.post("/register");
ResponseBody rbody=response.getBody();
String txtBody=rbody.asString();
System.out.println("Status line is :"+ response.statusLine());
System.out.println("Status code is :"+ response.statusCode());
System.out.println("*******************************************************");
System.out.println("Response body :");
System.out.println(txtBody);
System.out.println("*******************************************************");
System.out.println("All header information are given below:");
System.out.println(response.getHeaders());
}
}
Output of the above code if successfully done the entry in the database.
In case, if user details already exist then below output we will get from the server.
In case, if wrong keys used in the JSON file (like instead of 'FirstName' we are giving 'FirtName") then below output we will get from the server:
In case, if wrong HTTP method used like instead of POST method if we use "GET" method while sending request to the server then below out will come:
Please comment if you have any queries on this.
Best Of Luck!
No comments:
Post a Comment