rest-assuredasiformdatarequest

When pass the form parameters to the request in REST Assured didn't get the expected response


I need to create REST Assured api code using java for the below api configuration :

enter image description here

My Code is as below :

// First convert data table to the String map. Map<String, String> map = tableData.asMap(String.class, String.class);

    // We need headers params for this request. So first we are making headers params map for passing it to the request.
    HashMap<String, String> header_params = new HashMap<>();
    header_params.put("x-auth-token", getValueFromPropertyFile(PropertyFileKeys.X_AUTH_TOKEN));

    // Add content type as form-data.
    header_params.put("Content-Type", "application/json");
        // Get the SIP Device number based on the customer id.
        String sip_device_number = FaxerUsefullResource.getSIPDeviceNumberForCustomerAccount(admin_login_token,customer_id);
        print("SIP Device number of customer account id : "+customer_id+" is : " + sip_device_number);

        // Build the request specification.
        RequestSpecification request_specification_builder = new RequestSpecBuilder()

                // Set the Base Uri. Value of baseURL we are taking from the "config.properties" file.
                .setBaseUri(getValueFromPropertyFile(PropertyFileKeys.BASE_URL))

                // Set up the header params.
                .addHeaders(header_params)

                // Build entire request specification.
                .build();

        // Make the Request specification from the built request specification.
        request_specification = given()

                // Bind the built configuration with request.
                .spec(request_specification_builder)

                // Add form data parameter with values.
                .formParam("id","1")
                .formParam("token",admin_login_token)
                .formParam("action","faxer_create")
                .formParam("sip_device",sip_device_number)
                .formParam("fax_number",map.get("fax_number"))
                .formParam("fax_type",map.get("fax_type"))
                .formParam("description",map.get("description"));

        // Calling the request and store the response for further verification.
        response = hit_https_request_and_return_response("FaxerAPI", API_method, request_specification);

But when run the code everytime i got the session issue. That i am logged out.

SIP Device number of customer account id : 412 is : 2540285577
Resource name : FaxerAPI
End point : /admin/faxer/
Request method: POST
Request URI:    https://alpha.astppbilling.org/admin/faxer/
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    id=1
                token=OTlEckI5QjJBb3dCMy9vM0EwZDM2dz09
                action=faxer_create
                sip_device=2540285577
                fax_number=0001
                fax_type=1
                description=APIAutomationDescription
Path params:    <none>
Headers:        x-auth-token=PRLgav3UWUAkh5OAL6zL6EizBuRm37Ok
                Accept=*/*
                Content-Type=application/json
Cookies:        <none>
Multiparts:     <none>
Body:           <none>
HTTP/1.1 400 
Server: nginx/1.18.0
Date: Wed, 09 Nov 2022 06:16:10 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: ITPLATPci_session=a%3A0%3A%7B%7D; expires=Tue, 09-Nov-2021 16:16:10 GMT; Max-Age=0; path=/
Set-Cookie: ITPLATPci_session=B2ECYFUzBGkHeABzUTUFOAM7Vj4HJVEgUmJQJQIlAm0GNFNtBAxRalEyVC4BaQAkAD4AM1Q4ADZWfF1tUWACNFdkUm0FNwEzAWxTNwM1UTIHOQI5VTcEZAc0AGZRPwU6Az9WMgc%2BUTVSY1BvAm8CYgZvU2IEYlFlUWFULgFpACQAPgAxVDoANlZ8XWZRIQJfV2NSMAU1AXUBOVNwAydRIQc7AilVPARiBzcAOlEtBTgDOlYzBylRYVI1UGUCeAIyBm9TLQRiUTpRZVQuAWkAJAA%2BADFUOgA2VnxdelEiAmVXcFILBTABYAE5U20DIFEhBzsCKVU8BGAHOgA6US0FSAN7VmUHZFE7UmJQewIeAnAGL1NzBBBRb1E%2FVGkBPAAjACsANFQkADlWcF0%2BUWICIFcqUh4FMAFxAT1TLANlUTIHLgJqVSgEYgcwAClRLQUyA3hWPgc2UWBSPVB0AjoCZQYoU3cEDFFiUTVUeAE7ACEAbQB0VHMALlZlXWZRawIxVzRSYgVmAT4Ba1M3A2FRNAcwAmFVdQRpBzoAOlEtBXwDeFZhB3VRDFJjUDcCIgJlBnlTOAQgUTlRZlQ2AXAAdQA%2FAH0%3D; expires=Wed, 09-Nov-2022 08:16:10 GMT; Max-Age=7200; path=/
Set-Cookie: ITPLATPci_session=AWdXNVYxCmdQLwR3UjYHOggwUztRc1EgUGBWIw0qUD9dbwE%2FBw8FPlEyWSNaMgouBTsAM1Y6BzECKFRkBzYHMQo5CjUENlFjVDkBZQE3DG8BP1dsVjQKalBjBGJSPAc4CDRTN1FoUTVQYVZpDWBQMF00ATAHYQUxUWFZI1oyCi4FOwAxVjgHMQIoVG8HdwdaCj4KaAQ0USVUbAEiASUMfAE9V3xWPwpsUGAEPlIuBzoIMVM2UX9RYVA3VmMNd1BgXTQBfwdhBW5RZVkjWjIKLgU7ADFWOAcxAihUcwd0B2AKLQpTBDFRMFRsAT8BIgx8AT1XfFY%2FCm5QbQQ%2BUi4HSghwU2BRMlE7UGBWfQ0RUCJddAEhBxMFO1E%2FWWRaZwopBS4ANFYmBz4CJFQ3BzQHJQp3CkYEMVEhVGgBfgFnDG8BKFc%2FVisKbFBnBC1SLgcwCHNTO1FgUWBQP1ZyDTVQN11zASUHDwU2UTVZdVpgCisFaAB0VnEHKQIxVG8HPQc0CmkKOgRnUW5UPgFlAWMMaQE2VzRWeA%3D%3D; expires=Wed, 09-Nov-2022 08:16:10 GMT; Max-Age=7200; path=/
Expires: Tue, 01 Jan 2000 00:00:00 GMT
Last-Modified: Wed, 09 Nov 2022 06:16:10 GMT
Cache-Control: no-store, no-cache, must-revalidate, max-age=0
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache

{
    "status": false,
    "error": "You are already logged out. Please login again",
    "response_code": 400
}

How can i resolved this ?


Solution

  • Try changing the content-type header to 'application/x-www-form-urlencoded' instead of 'application/json'

    You can also check this article for more details: https://www.baeldung.com/postman-form-data-raw-x-www-form-urlencoded