Search code examples
javasession-cookiesrestful-authenticationrest-assuredrest-assured-jsonpath

Getting HTTP 401 error while trying to validate the connection.I am using rest assured to automate


/*I have passed session cookies manually in below code.when i pass the session cookies in post man its working fine but in eclipse it giving 401 HTTP error.I am very new to rest assured.please let me know if anything is wrong with the code.I have attached the error aswell enter image description here */

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

import java.security.KeyStore;
import java.security.cert.X509Certificate;

import javax.security.cert.CertificateException;

import org.apache.http.HttpResponse;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.testng.annotations.Test;

import com.sun.net.ssl.SSLContext;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
public class getart {

    @Test

    public void Test1()
    {
        // TODO Auto-generated method stub

        //baseURL or Host

        RestAssured.useRelaxedHTTPSValidation();
        RestAssured.baseURI="https://localhost";

        given().

            param("Cookie","JSESSIONID=B1FAC334FF60F7182D4C552ABE01A700; hi.session.co.entity=1838-PROD1; hi.session.id.identifier=xHmvClBuIBcSKAEiVP~~AAAESADWaUjq; hi.session.client.identifier=1838Viewer").
            when().
            get("/hi-prod/3.1.12/al/api/articles")
           .then().assertThat().statusCode(200).
            body("status",equalTo("OK")).log().body();


    }

}  

Solution

  • param() is used to add a query parameter to the URL - which is not something you want for setting the cookies. Cookies have to be set in the Request Header.

    Instead you should be using cookie() or cookies(). The rest-assured wiki has a section on how to set the cookies. Note that you will need to set four cookies according to your example.

    EDIT: Added as per comment,

    Approach 1

    Cookie cookie1 = Cookie.Builder("JSESSIONID", "B1FAC334FF60F7182D4C552ABE01A700").build();
    Cookie cookie2 = Cookie.Builder("hi.session.co.entity", "1838-PROD1").build();
    Cookie cookie3 = Cookie.Builder("hi.session.id.identifier", "xHmvClBuIBcSKAEiVP~~AAAESADWaUjq").build();
    Cookie cookie4 = Cookie.Builder("hi.session.client.identifier", "1838Viewer").build();
    Cookies cookies = new Cookies(cookie1, cookie2, cookie3, cookie4);
    
    given().cookies(cookies)
           .when().get("/hi-prod/3.1.12/al/api/articles")
    

    Approach 2

    given().header("Set-Cookie", "JSESSIONID=B1FAC334FF60F7182D4C552ABE01A700; hi.session.co.entity=1838-PROD1; hi.session.id.identifier=xHmvClBuIBcSKAEiVP~~AAAESADWaUjq; hi.session.client.identifier=1838Viewer"")
           .when().get("/hi-prod/3.1.12/al/api/articles")