How to handle a GET request for REST API request which is having a body with QAF Webservice

101 views
Skip to first unread message

prasha...@gmail.com

unread,
May 11, 2021, 2:10:22 AM5/11/21
to qaf users
Hi Chirag,

I am using QAF webservice for API automation. I have a case where GET request has a body present. If I pass the request using either properties file or xml file, on executing I am getting 404 not found response. I have another scenario where GET request does not have a body where the execution happens without any issues. If a GET request has a body then we are facing this 404 response  issue.Upon debugging, found that Jersey API client at the end changes the request from GET to POST if the GET request has a body. Please let me know how to handle this scenario from QAF Webswrvice.

Thanks

cjayswal

unread,
May 13, 2021, 5:23:05 PM5/13/21
to qaf users
This is not a usual case and Jersey library replaces GET method to POST. As a solution you can try with Apache HttpClient. Below is the example:


package qaf.example.tests;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;

import com.qmetry.qaf.automation.ws.rest.RestClientFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.client.apache.ApacheHttpClient;
import com.sun.jersey.client.apache.ApacheHttpClientHandler;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

/**
 * @author chirag
 *
 */
public class ApacheClientProvider extends RestClientFactory {

    @Override
    protected Client createClient() {
        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.getParams().setConnectionTimeout(5000);
        connectionManager.getParams().setSoTimeout(1000);
        connectionManager.getParams().setDefaultMaxConnectionsPerHost(10);
        HttpClient httpClient = new HttpClient(connectionManager);
        ApacheHttpClientHandler clientHandler = new ApacheHttpClientHandler(httpClient);
        ClientHandler root = new ApacheHttpClient(clientHandler );
        ClientConfig config = new DefaultApacheHttpClientConfig();
        Client client = new Client(root, config);
        return client;
    }

}

in order to use it register your class using rest.client.impl property, in above case:

rest.client.impl=qaf.example.tests.ApacheClientProvider

Hope this will help.
મંગળવાર, 11 મે, 2021ના રોજ 02:10:22 AM UTC-4 વાગ્યે prasha...@gmail.com દ્વારા આમ લખવામાં આવ્યું હતું:

prasha...@gmail.com

unread,
May 26, 2021, 10:41:06 AM5/26/21
to qaf users
Hi Chirag,

When I tried to execute after making the above changes, I am getting 401 unauthorized error.Looks like the basic authentication property  which we provide in application.properties doesn't work after we override with above class. Please let me know on how to proceed with this. 

Thanks

Karan Suri

unread,
May 27, 2021, 12:42:17 AM5/27/21
to qaf-...@googlegroups.com
Hello Prashanth,

you can try below workaround. We are using the same in our projects.

Create one class for get method and inherit PostMethod in that class.

public class HttpGetWithEntity extends PostMethod {

public HttpGetWithEntity(String uri) {
super(uri);
}

@Override
public String getName() {
return "GET";
}

}

download com.sun.jersey.client.apache.ApacheHttpClientHandler java file and put it in your repository.

modify getHttpMethod as below,

private HttpMethod getHttpMethod(ClientRequest cr) {
final String strMethod = cr.getMethod();
final String uri = cr.getURI().toString();

if (strMethod.equals("GET")) {
return new HttpGetWithEntity(uri);
} else if (strMethod.equals("POST")) {
return new PostMethod(uri);
} else if (strMethod.equals("PUT")) {
return new PutMethod(uri);
} else if (strMethod.equals("DELETE")) {
return new CustomMethod("DELETE", uri);
} else if (strMethod.equals("HEAD")) {
return new HeadMethod(uri);
} else if (strMethod.equals("OPTIONS")) {
return new OptionsMethod(uri);
} else {
return new CustomMethod(strMethod, uri);
}
}

create another class to implement creatClient method,

public class QAFApacheHttpClient extends RestClientFactory {
@Override
protected Client createClient() {
HttpClient httpClient = new HttpClient();
ApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
ApacheHttpClientHandler clientHandler = new ApacheHttpClientHandler(httpClient, config);
ClientHandler root = new ApacheHttpClient(clientHandler);

Client client = new Client(root, config);
return client;
}
}

In application.properties, set below property,

rest.client.impl=Fully Qualified name of QAFApacheHttpClient class.

Regards,
Karan


--
You received this message because you are subscribed to the Google Groups "qaf users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qaf-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qaf-users/654a8fb5-5ae2-4f15-b19d-ed0181f7ddbfn%40googlegroups.com.

cjayswal

unread,
May 27, 2021, 12:56:38 PM5/27/21
to qaf users
In that case add basic authentication to appache client . Here is the example as per appache client documentation.

Credentials defaultcreds = new UsernamePasswordCredentials(
                ConfigurationManager.getBundle().getString("rest.client.basic.auth.username",""),
                ConfigurationManager.getBundle().getString("rest.client.basic.auth.password", ""));

httpClient.getState().setCredentials(new AuthScope("myhost", 80, AuthScope.ANY_REALM), defaultcreds);


Alternately, you can add filter to jersey client. Below is example of adding filter in above code:

        client.addFilter(new HTTPBasicAuthFilter(
                ConfigurationManager.getBundle().getString("rest.client.basic.auth.username",""),
                ConfigurationManager.getBundle().getString("rest.client.basic.auth.password", "")));


બુધવાર, 26 મે, 2021ના રોજ 10:41:06 AM UTC-4 વાગ્યે prasha...@gmail.com દ્વારા આમ લખવામાં આવ્યું હતું:
Reply all
Reply to author
Forward
0 new messages