New to the RestAssured and just checking different ways of working with REST APIs. For testing I am using http://dummy.restapiexample.com/api/v1. In this I am trying the GET employee method using RequestSpecification and groovy way but I am getting different response.
My short code is:
RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1";
RequestSpecification request = RestAssured.given();
Response response = request.get("/employee/72100");
System.out.println(response.getBody().asString());
given().baseUri("http://dummy.restapiexample.com/api/v1").get("/employee/72100").then().log().body();
And output I am getting is:
{"id":"72100","employee_name":"mpr51_0280","employee_salary":"123","employee_age":"23","profile_image":""}
<html>
<body>{"id":"72100","employee_name":"mpr51_0280","employee_salary":"123","employee_age":"23","profile_image":""}</body>
</html>
I am not getting why it is returning response with HTML tags. Can anyone explain or give hint to get same response as first call to get method.
NOTE: You may or may not get details for employeeID 72100
You can use any employee ID from response of:
http://dummy.restapiexample.com/api/v1/employees
Because the Body contains it .
You can use below statement if you need only the response
given().baseUri("http://dummy.restapiexample.com/api/v1").get("/employee/72100").then().log();
OR
Response resp = given().baseUri("http://dummy.restapiexample.com/api/v1").get("/employee/72100");
System.out.println(resp.asString());
Related
I am a newbie to Rest Assured and need your help on the following issue.
I want to trigger a POST request which is as follows:-
Response resp = RestAssured.given().pathParam("build", bulid).
formParam("file", "https://unsplash.com/photos/Bcv4wZSMtIA").// Cast
formParam("type", "front").
formParam("auto_start", false).
then().post("https://example.com/{build}");
Where build= abc/xyz
Thus. should result in https://example.com/abc/xyz as endpoint and body as:
{
"file" : "https://unsplash.com/photos/Bcv4wZSMtIA",
"type" : "front",
"auto_start" : false
}
But when triggered it gives 404, whereas when instead of using pathparam I hardcode the value of build in post request then it works fine.
Can someone please advise what am I doing wrong here.
Found out that the url was being encoded when sent as request uri which when disabled using used urlEncodingEnabled(false) resolved my issue.
Considering that in my Zend Framework module I'm able to access via browser or REST. When I got a 404 via REST, this is the content:
{"httpStatus":404,"title":"Not Found"}
So how do I test this response using PHPUnit?
I have already a test for another route that is working:
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/ws/dashboard');
$this->assertResponseStatusCode(200);
$this->assertModuleName('Ws');
$this->assertControllerName('Ws\Controller\Dashboard');
$this->assertControllerClass('DashboardController');
$this->assertMatchedRouteName('application/ws');
}
But I don't know how to do this other case.
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/ws/asd');
$this->assertResponseStatusCode(404); // this is OK
// but how check the json response?
}
Also, if I do this $response = $this->getResponse(); I got the entire HTML 404 html file, which is not what I want.
You may want to check to ensure that the controller is actually returning a JSON response.
Also, your Accept header for the AJAX request should specify application/json.
I have a Grails controller that receives a DefaultMultipartHttpServletRequest like so:
def myController() {
DefaultMultipartHttpServletRequest proxyRequest = (DefaultMultipartHttpServletRequest) request
}
This controller acts as a proxy by taking pieces of this request and then resends the request to another destination.
For non-multipart requests, this worked fine, I did something like:
IProxyService service = (IProxyService) clientFactory.create()
Response response = service.doPOST(proxyRequest.getRequestBody())
Where proxyRequest.getRequestBody() contains a JSON block containing the request payload.
However, I do not know how to get this to work with multipart request payload, since the request body is no longer a simple block of JSON, but something like the following (taken from Chrome devtools):
How can I can pass this request payload through using my proxy service above, where doPost takes a String?
Have you tried
def parameterValue = request.getParameter("parameterName")
to get the parameter value?
If you see the method signatures for DefaultMultipartHttpServletRequest you will see there are methods for getting the files and other parameters separately because the request body is getting used to both upload the file and to pass in other parameters.
How do I invoke a Service Operation in WCF from iOS?
I have a Service Operation defined in my WCF Data Service (tied to a stored procedure in my DB schema) that I need to invoke from iOS. Say I've got the following declaration in my .svc.cs file:
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public IQueryable<Foo> GetFoos(int param1, DateTime param2, string param3)
{
return CurrentDataSource.GetFoos(param1, param2, param3).AsQueryable();
}
And I've got it set up with the proper rights in InitializeService:
config.SetServiceOperationAccessRule("GetFoos", ServiceOperationRights.AllRead);
When I try to invoke this via HTTP POST from iOS, I get back an error wrapped in JSON:
Bad Request - Error in query syntax.
It seems like it doesn't like how I'm passing my parameters. I'm passing them JSON-encoded (using NSJSONSerialization to turn an NSDictionary into a JSON string) in the request body of a POST request. The same method works on another web service (.svc) not connected to WCF that has operations annotated the same way.
An answer to another question of mine in a similar vein suggests that data formats can be negotiated between client and server, and I've read that dates are a pain to format, so maybe it's my DateTime parameter that's a problem. But I've tried both the JSON format (\/Date(836438400000)\/ and /Date(836438400000)/) and the JSON Light format (1996-07-16T00:00:00) to no avail.
So my question is this: what is the proper way to invoke this operation? If I need to have my app tell the server what format to expect, how do I do that?
Update: I tried using the format datetime'1996-07-16T00:00:00' as mentioned in this question. Same error.
Update 2: The MSDN page for Service Operations seems to suggest that nothing besides Method = "POST" is supported when annotating the WebInvoke for a Service Operation. I tried removing everything from what is quoted in the above code and setting the method to POST. Same error.
Update 3: On Pawel's suggestion, I made a new Service Operation on my Data Service just like this:
[WebInvoke(Method = "POST")]
public IQueryable<string> GetFoos()
{
List<string> foos = new List<string>();
foos.Add("bar");
return foos.AsQueryable();
}
I was able to make it work in Fiddler's Composer pane by setting the method to POST, adding accept:application/json;charset=utf-8 and Content-Length:0 to the headers. Then I added a single int parameter to the operation (called param1). I set the body of my request in Fiddler to {"param1":"1"} and ran it (and Fiddler automatically updated my content-length header), and got the same error. I changed the type of my parameter to string and ran my request again and it worked. So my problem seems to be non-string types.
You need to send parameters in the Url and not in the request body.
I am trying to have a server respond to a request which needs a XML structure. The easiest way I thought would be to create a POST with a string containing the XML, using the Play Framework.
However, I cannot seem to get it to work. I am calling the test with the following code:
Map<String,String> map = new HashMap<String,String>();
map.put("data", xmlString);
Http.Response response = POST("/server/", map);
When on the server checking the parameters it is not in it as it returns false:
params._contains("data")