0

I am trying to run this script for rest-assured, the last line uses a hamcrest assertion to validate the response body:

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class Basics {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Validate Add location API
    
    
    // Setting up base URI
    RestAssured.baseURI = "https://rahulshettyacademy.com";
    
    //given : all inputs wrapped under given method // when : hit the API // then : validate the response


    //add location
    
    String response = given().log().all().queryParam("key", "qaclick123")
            .header("Content-Type","application/json")
            .body(payLoad.addLocation())
    .when().post("/maps/api/place/add/json")
    .then().assertThat().statusCode(200)
            .body("scope", equalTo("APP"))
            .header("Server",("Apache/2.4.41 (Ubuntu)"))
            .extract().response().asString();

    System.out.println(response);

    //using JasonPath for parsing json

    JsonPath js = new JsonPath(response);
    String place_id = js.getString("place_id");
    System.out.println(place_id);

    //update location

    given().log().all().queryParam("key","qaclick123")
            .header("Content-Type","application/json")
            .body("{\n" +
                    "\"place_id\":\""+place_id+"\",\n" +
                    "\"address\":\"70 Summer walk, USA\",\n" +
                    "\"key\":\"qaclick123\"\n" +
                    "}")
            .when().put("/maps/api/place/add/json")
            .then().log().all().assertThat()
            .statusCode(200).body("msg",equalTo("Address successfully updated"));

  }
}

It goes all well untill the last assertion to validate the response body, at the other place with the POST request the assertion is validating the response body but in the PUT request throws me this error:

Request method: POST
Request URI:    https://rahulshettyacademy.com/maps/api/place/add/json?key=qaclick123
Proxy:          <none>
Request params: <none>
Query params:   key=qaclick123
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
            Content-Type=application/json
Cookies:        <none>
Multiparts:     <none>
Body:
{
    "location": {
        "lat": -38.383494,
        "lng": 33.427362
    },
"accuracy": 50,
"name": "Frontline house",
"phone_number": "(+91) 983 893 3937",
"address": "29, side layout, cohen 09",
"types": [
    "shoe park",
    "shop"
],
"website": "http://google.com",
"language": "English-IN"
}
{"status":"OK","place_id":"e50a637ebdf651c6808204513eedadd1","scope":"APP","reference":"ef6cc67ffac92ea58e901ee1bfe02bf5ef6cc67ffac92ea58e901ee1bfe02bf5","id":"ef6cc67ffac92ea58e901ee1bfe02bf5"}
e50a637ebdf651c6808204513eedadd1
Request method: PUT
Request URI:    https://rahulshettyacademy.com/maps/api/place/add/json?key=qaclick123
Proxy:          <none>
Request params: <none>
Query params:   key=qaclick123
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
                Content-Type=application/json
Cookies:        <none>
Multiparts:     <none>
Body:
{
    "place_id": "e50a637ebdf651c6808204513eedadd1",
    "address": "70 Summer walk, USA",
    "key": "qaclick123"
}
HTTP/1.1 200 OK
Date: Thu, 10 Nov 2022 16:56:27 GMT
Server: Apache/2.4.41 (Ubuntu)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json; charset=UTF-8
Exception in thread "main" java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty.
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:73)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:108)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:58)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:277)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper.parseText(ConfigurableJsonSlurper.groovy:80)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper$parseText.call(Unknown Source)
    at io.restassured.internal.ContentParser.parse(ContentParser.groovy:42)
    at io.restassured.internal.ContentParser$parse.call(Unknown Source)
    at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:497)
    at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
    at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:696)
    at io.restassured.internal.ResponseSpecificationImpl.this$2$validateResponseIfRequired(ResponseSpecificationImpl.groovy)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:198)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:62)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
    at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:270)
    at io.restassured.specification.ResponseSpecification$body$1.callCurrent(Unknown Source)
    at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:117)
    at io.restassured.internal.ValidatableResponseOptionsImpl.body(ValidatableResponseOptionsImpl.java:244)
    at Basics.main(Basics.java:50)

Please help me understand what am I missing here

4
  • 1
    pls show the last response. if response is null or empty --> throws this exception. Commented Nov 11, 2022 at 2:11
  • 1
    the content-length of your response is 0. Is that how it is expected to be? Commented Nov 11, 2022 at 8:49
  • @AlexeyR. thanks for your response, I forgot to mention in postman we get a response: { "msg": "Address successfully updated" } its a dummy setup though Commented Nov 11, 2022 at 13:55
  • @lucas-nguyen-17 i checked the response-length is zero but in postman we get a json response with success message Commented Nov 11, 2022 at 14:21

1 Answer 1

1

URL of PUT request should be

https://rahulshettyacademy.com/maps/api/place/update/json?key=qaclick123

You used URL of POST request for PUT request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.