0
votes

I practicing web services using RestAssured. In the post request, it return 500 server error where i using postman to send the request and there is no error. Can anyone help me debug the error? Please give some opinion. Any help is appreciate? Thanks in advance.

@BeforeSuite
    public void setup() {
        RestAssured.reset();
        ProxySpecification ps = new ProxySpecification("localhost", 8080, "http");
        RestAssured.proxy(ps);

        RestAssured.baseURI = "https://jsonplaceholder.typicode.com/";
        RestAssured.port = 443;
    }

@Test(groups="createUser")
    public void createUser() {
        given()
            .param("userId", 1234)
            .param("id", 82342)
            .param("title", "Senior Software Engineer")
            .param("body", "Scrum Org").
        when()
            .request("POST", "/posts").
        then()
            .statusCode(200);

    }

Error Message is Expected status code <200> but was <500>.

Stacktrace:

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83) at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77) at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:238) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:250) at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:483) at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128) at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:655) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169) at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:123) at io.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169) at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:131) at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:119) at com.peterwkc.services.Main.createUser(Main.java:75)

1
Which line is line 75 in Main.java? The 500 means internal server error, so some exception occurred during the creation of the response. - Mark
.statusCode(200); This line. - nicholas
Like @Mark is saying, it looks like a typo or parameter mismatch that is causing the server error. Can you also show us the Postman request? With the Code link on postman, you can output the Postman request as HTTP. - AutomatedChaos

1 Answers

1
votes

You have to send JSON body instead of parameters. Then your request will look like this:

given()
    .contentType(ContentType.JSON)
    .body("{"
        + "\"userId\": 1234,"
        + "\"id\": 823823,"
        + "\"title\": \"Title\","
        + "\"body\": \"Scrum\""
        + "}"
    )
    .when()
    .request("POST", "/posts").
    then()
    .statusCode(201);

There is official wiki page with a lot of useful examples here: https://github.com/rest-assured/rest-assured/wiki