I'm using the SoapUI API as part of an existing java project.
The application should save the request and response XML in an specific report file.
I wonder if it's possible to get those requests and responses via the API.
The method invoking the TestCaseRunner looks like this
protected void checkTestCase(TestCase testCase) {
TestCaseRunner tr = testCase.run(null, false);
for (TestStepResult tcr : tr.getResults()) {
String status = tcr.getStatus();
String time = tcr.getTimeTaken() + "ms";
/* How to get XML messages?
* String request =
* String response =
*/
}
}
Depending on exactly what kind of test steps you have they might be an instance of a MessageExchange. Casting the TestStepResult to a MessageExchange and calling getRequestContent / getResponseContent might do the trick.
String request = ((MessageExchange)tcr).getRequestContent();
String response = ((MessageExchange)tcr).getResponseContent();
I have used the following way to get the response from the API CAll performed:
runner = testRunner.runTestStepByName("Your Test Case name");
// Here we take the response in ms of the API call
timeTaken = runner.response.timeTaken;
// here we get the HTTP response code.
responseCode = runner.getResponseHeaders()."#status#";
// here we get the response content
String response = runner.getResponseContent();
// here we get the API call endpoint -> in case you need to print it out.
String endPoint = runner.getEndpoint();
Related
I am trying to capture the graphql response from the network response tab using the selenium4 dev tool.
I have tried the below code and its prints all data from the 30 + requests available on the network response but I just want to fetch graphql file and print the response available with graphql request.
DevTools finalDevTools = devTools;
devTools.addListener(Network.responseReceived(), responseReceived -> {
requestIds[0] = responseReceived.getRequestId();
String url = responseReceived.getResponse().getUrl();
int status = responseReceived.getResponse().getStatus();
String type = responseReceived.getType().toJson();
String headers = responseReceived.getResponse().getHeaders().toString();
String responseBody = finalDevTools.send(Network.getResponseBody(requestIds[0])).getBody();
System.out.println(responseBody);
});
I am trying to automate twitter API. when tried to print "js.get("text") using
System.out.println(js.get("text")); I am getting error as
"The method println(boolean) is ambiguous for the type PrintStream"
I downloaded jars and passed in Build path as well "scribejava-apis-2.5.3" and "scribejava-core-4.2.0"
Below code is not allowing me use println for ------>js.get("text")
public class Basicfunc {
String Consumerkeys= "**************";
String Consumersecretkeys="*******************";
String Token="*******************";
String Tokensecret="***************************";
#Test
public void getLatestTweet(){
RestAssured.baseURI = "https://api.twitter.com/1.1/statuses";
Response res = given().auth().oauth(Consumerkeys, Consumersecretkeys, Token, Tokensecret).
queryParam("count","1").
when().get("/home_timeline.json").then().extract().response();
String response = res.asString();
System.out.println(response);
JsonPath js = new JsonPath(response);
System.out.println(js.get("text"));
}
}
Use System.out.println(js.getString("text")); instead of System.out.println(js.get("text"));, because get returns any primitive value.
I think your problem is that your twitter response is actually a list.
Try to use System.out.println(js.getList()[0].get("text")); and be aware that you are only using the first [0] entry and ignoring the rest.
I'm very new to this, like really, really new.
I'm using Spring MVC (5.0) and am making an ajax call, as shown below.
This all works fine.
#RestController
public class AjaxController
{
#RequestMapping(value="/search/users", method=RequestMethod.GET)
public Person getUsers(#RequestParam("username") String username)
{
persons = personService.findPersonByUsername(username);
return persons.size() == 0 ? null : persons.get(0);
}
}
The method gets the person from the database and returns it.
According to the Spring Restful guide,
"The XXX object must be converted to JSON.Thanks to Spring’s HTTP message converter support, you don’t need to do this conversion manually. Because Jackson 2 is on the classpath, Spring’s MappingJackson2HttpMessageConverter is automatically chosen to convert the XXX instance to JSON."
So, Spring is automatically generating the JSON which will be returned to the client. The problem is, I get an error message in my client javascript:
SyntaxError: JSON.parse: unterminated string literal at line 1 column 103911 of the JSON data
My client javascript is equally simple, consisting of only:
function ajax_get_users(input_box)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "users?username=" + username, true); // url of server-side ajax script, specify synchronous ajax call
// get asynchronous response
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var person = JSON.parse(this.responseText); // this is where the unterminated String error occurs
}
};
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // set http header
xhttp.send(); // send the ajax request to the web server
}
Since Spring is constructing the JSON, how do I even fix the problem?
I'm sending a http post request from javascript, with some json data.
Javascript
var data = {text : "I neeed to store this string in database"}
var xhr= new XMLHttpRequest();
xhr.open("POST","http://localhost:9000/postJson" , true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.send(data);
xhr.setRequestHeader("Connection", "close");
//Also, I've tried a jquery POST
//$.post('postJson', {'data=' : JSON.stringify(data)});
//But this doesn't make a request at all. What am I messing up here?
Route
POST /postJson controllers.Application.postJson()
Controller
public static Result postJson(){
//What should I write here to get the data
//I've tried the below but values is showing null
RequestBody rb=request().body();
final Map<String,String[]> values=rb.asFormUrlEncoded();
}
What is the way to parse the POST request body?
Much thanks!
Retreive the request body directly as JSON... no need to complicate your life.
public static Result postJson() {
JsonNode rb = request().body().asJson();
//manipulate the result
String textForDBInsertion = rb.get("text").asText(); //retreives the value for the text key as String
Logger.debug("text for insertion: " + textForDBInsertion
+ "JSON from request: " + rb);
return ok(rb);
}
Also, I recommend you use the AdvancedRestClient Chrome plugin for testing. This way you can eliminate from the equation client-side code errors.
Cheers!
I am trying to query Solr using certain fields and I want the response in XML format. Somehow I am not able to get the response in XML format even though I have set the parser to XMLResponseParser. Please check the code and let me know what is wrong in here:
HttpSolrServer solr = new HttpSolrServer(urlString);
String queryString ="*:*";
SolrQuery query = new SolrQuery(queryString);
query.setQuery(queryString);
query.setFields("type", "typestring");
query.addFilterQuery("id");
query.setStart(0);
query.setRows(100);
solr.setParser(new XMLResponseParser());
QueryResponse resp = solr.query(query);
SolrDocumentList results = resp.getResults();
for (int i = 0; i < results.size(); ++i) {
// I need this results in xml format
System.out.println(results.get(i));
}
Your code is using SolrJ as a Solr client. It's precisely done to avoid dealing with XML responses, and it provides a clean way to get Solr results back in your code as objects.
If you want to get the raw xml response, just pick up any java HTTP Client, build the request and send it to Solr. You'll get a nice XML String...
NOTE : You can use ClientUtils.toQueryString(SolrParams params, boolean xml) to build the query part of your URL
As Grooveek already wrote, SolrJ is intended to take XML parsing away from you as a user of the library. If you want to see the XML, you need to fetch the response on your own.
SolrQuery query = new SolrQuery("*:*");
// set indent == true, so that the xml output is formatted
query.set("indent", true);
// use org.apache.solr.client.solrj.util.ClientUtils
// to make a URL compatible query string of your SolrQuery
String urlQueryString = ClientUtils.toQueryString(query, false);
String solrURL = "http://localhost:8080/solr/shard-1/select";
URL url = new URL(solrURL + urlQueryString);
// use org.apache.commons.io.IOUtils to do the http handling for you
String xmlResponse = IOUtils.toString(url);
// have a look
System.out.println(xmlResponse);