Is it possible to extract the method name from the response object? - java

I'm using REST-Assured in Java and here's how I'm getting my response object:
Response response = RestAssured.given().contentType(ContentType.JSON).header(header_name).get();
I want to know if there's any way to extract the method name used (GET in this case) from the response object.

Incase if you're interested in knowing the requested method say GET or POST, below code will print the method on the console
given().log().method()
.when()
.get("https://www.google.co.in/").then().statusCode(200);
Hope this helps

Related

How can I get the API response in TestNG using Rest Assured to print out line by line?

I created a program to get an API response from a URL.
But for some reason it's printing it out in one long line. Is there any way to print it out the way I see it in postman? I guess what I mean is if there is a way to see the response from the API server printed out line by line instead of one long line.
ResponseBody body = response.getBody();
System.out.println("Response Body is: " + body.asString());
The server response is
[RemoteTestNG] detected TestNG version 6.13.1
Status code is 200
Response Body is:
{"request_id":"Z36ec5ee76a4788bfe83655edbbe9f0","status":"OK","data":{ONE LONG STRING OF DATA WITH NO END IN SIGHT!}
You can use prettyPrint method of Response class. Status you will have to print.
(Response to comment)
If your API call return JSON responses, you can use a JSON validator module.
What it does is: you provide a JSON schema, and it compares it with the response. The JSON schema syntax is defined over there: http://json-schema.org/latest/json-schema-validation.html (it looks more complex than it actually is) and here are some examples http://json-schema.org/examples.html. You can define, in your schema, if a field is "required", and also which "type" it should be (string, integer etc.) and many other things!
Here's a simple tutorial that helped me implement it with Rest-Assured: https://blog.jayway.com/2013/12/10/json-schema-validation-with-rest-assured/

Separate GET parameters from POST in embedded Jetty server

I need to know wheather the parameter is GET or POST but in handle method request.getParameter(name) gives me all parameters. Is it possible to do something like request.getGETParameter(name) and request.getPOSTParameter(name) or do I have to parse raw data myself?
There is no such thing as GET parameters and POST parameters. GET and POST are methods of HTTP request.
You can find out which method your request is by calling
public String getMethod();
on your request.
You might also want to take a look on the description of HTTP protocol http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
The difference between parameters being sent in GET in and POST method is that in GET request parameters are sent in query string, and in POST request these are sent in request body.

What return me request.getParameter() when I send a GET and/or POST with the same name?

request.getParameter() to retrieve items sent by POST and/or GET.
String name = request.getParameter("name");
But how can we distinguish between these two cases? In fact, if I send both a post and get with the same name, what getParameter returns me?
An HTTP request can only have one method associated. So in your case, your request is either a GET request or a POST request, but not both. Note that other HTTP methods than GET or POST exists (OPTIONS, PUT, DELETE, ...).
If you want to know which method was used in your current request, you can use request.getMethod().
request.getParameter("name"); will return the value associated with HTML/JSP element having id="name"in both GET and POST cases.
If you want to check whether the request was GET or POST you can use request.getMethod();
You can use only one at a time GET or POST. And your statement retrieve value whose name/id="name". You can check method by request.getMethod(); Also GET is used for limited data around 2 kb and POST for unlimited data to be retrieved.

How to send a unix path as part of a JSON object to a web service via GET

Sorry if this is a duplicate question but google isn't smart enough to understand me or I'm too dumb to express my question simple enough for it to understand.
I don't know if this is my problem but I'm 90% sure this is it.
I'd like to know how to represent a Unix path within a GET request so that my web service doesn't return a 404. I think it's because one of my JSON fields in the query is a Unix path and because of the slashes, the webservice thinks it's part of the URL and not a part of my query.
For example, I'm using a RESTClient that's an add-on to Mozilla to test my web service. For the POST request, I enter as the url
http://mytestserver:8080/mydir/
and in the body, I put in my JSON object
{"filename":"obit.jpg", "Path":"test/2/1"}
This method works fine. I get a status code 200 and a return JSON object with the expected output.
When I use the same string for a GET request, I get a status code 404 and no return JSON object. I put as the url in the RESTClient
http://mytestserver:8080/mydir/{"filename":"obit.jpg", "Path":"test/2/1"}
and I get a status code 404 and the response body just says 404 - Not found
To further test my theory, I entered the following url in a GET request, removing the /2/1 from the path, which works.
http://mytestserver:8080/mydir/{"filename":"obit.jpg", "Path":"test"}
I've tried encapsulating the whole JSON string in quotes but that didn't work either so I've run out of things to try.
Thanks in advance for any help you can give me. If I need to show some code, please let me know, although, I don't think it's a code problem, I think it's a representation problem. Thanks.
Found out that JSON objects are usually sent via POST, not GET. Since I appended it to the URL via GET, it gave me problems. Per How to send a GET request with a "/" in the query

How do I get Rest Assured to return the text (non-encrypted or streamed) value in my REST response?

I recently moved over to Java and am attempting to write some REST tests against the netflix REST service.
I'm having an issue in that my response using rest assured either wants to send a gzip encoded response or "InputStream", neither of which provide the actual XML text in the content of the response. I discovered the "Accept-Encoding" header yet making that blank doesn't seem to be the solution. With .Net I never had to mess with this and I can't seem to find the proper means of returning a human readable response.
My code:
RestAssured.baseURI = "http://api-public.netflix.com";
RestAssured.port = 80;
Response myResponse = given().header("Accept-Encoding", "").given().auth().oauth(consumerKey, consumerSecret, accessToken, secretToken).param("term", "star wars").get("/catalog/titles/autocomplete");
My response object has a "content" value with nothing but references to buffers, wrapped streams etc. Trying to get a ToString() of the response doesn't work. None of the examples I've seen seem to work in my case.
Any suggestions on what I'm doing wrong here?
This has worked for me:
given().config(RestAssured.config().decoderConfig(DecoderConfig.decoderConfig().noContentDecoders())).get(url)
I guess in Java land everything is returned as an input stream. Using a stream reader grabbed me the data I needed.
Until its version 1.9.0, Rest-assured has been providing by default in the requests the header "Accept-Encoding:gzip,deflate" with no way of changing it.
See
https://code.google.com/p/rest-assured/issues/detail?id=154
It works for me:
String responseJson = get("/languages/").asString();

Categories