How to convert Odata request to JSON response - odata

The response has been returned as Iterable object. I need to return any response as json response.
Example, the below returns a different return type
ClientEntitySetIterator<ClientEntitySet, ClientEntity> iterator =
readEntities(edm, serviceUrl, "employee");
Instead of iterator need to return the content as JSON directly or retrieve the response as JSON without having any mapper or custom code.
Please advise.

Related

Different response for same API method

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());

How to parse values from a JSON field that is also a JSON?

I have a JSON that has one field that in itself contains another JSON. I am having trouble parsing values from this embedded JSON. I am using SwiftyJSON and can extract the embedded JSON as JSON type (swiftyJSON type). But I am unable to do anything with it further to get values from the embedded JSON.
thisjson[0]["MESSAGE_JSON_BODY"] will return a JSON type.
thisjson[0]["MESSAGE_JSON_BODY"].string will convert this JSON to string.
Now I need help parsing fields from MESSAGE_JSON_BODY JSON. How to parse the fields in the embedded JSON?
I figured out one way to solve this problem myself, here I print out the field value for "fieldkey":
if let stringdata: String = thisjson[0]["MESSAGE_JSON_BODY"].string{
var data: NSData = stringdata.dataUsingEncoding(NSUTF8StringEncoding)!
let msgjson = JSON(data: data)
println(msgjson["fieldkey"])
}

How to identify a JSON object using swift and xctest framework?

Using NSJSONSerialization.JSONObjectWithData(..) we can create a JSON object. But is it possible to identify the object type ie. is it a JSON object or not.
I was just trying to check using Swift and XCTestFramework. I tried different ways but no solution still?
Note: After creation of JSON object, I can get the values and can also check the values. XCTest Framework is working fine to test those type of things. But, I stuck to identify the object type.
Anybody has any idea how to identify the JSON object programmatically using Swift and XCTest framework
Update: For example, for a website testing we can do the following:
if let HTTPResponse = response as? NSHTTPURLResponse,
responseURL = HTTPResponse.URL,
MIMEType = HTTPResponse.MIMEType
{
XCTAssertEqual(responseURL.absoluteString, URL.absoluteString, "HTTP response URL should be equal to original URL")
XCTAssertEqual(HTTPResponse.statusCode, 200, "HTTP response status code should be 200")
XCTAssertEqual(MIMEType, "text/html", "HTTP response content type should be text/html")
} else {
XCTFail("Response was not NSHTTPURLResponse")
}
Is something possible like above for JSON?
If you get a valid string within your NSData parameter, but that String is not a valid JSON object, then the parser will throw an error. As from the documentation:
If an error occurs, upon return contains an NSError object that describes the problem.
So check if the object returned is actually an NSError. If you don't get an error, then I would safely assume that the object is indeed a valid JSON object.

how deserialize a json in rails

I send a http request and get a json for response like below:
{"total":"1200.0","used":"35.0","available":1165.0}
now I want deserialize this json and show to user in a table like below:
total : 1200
used : 35
availabele : 1165
I use JSON.parse(response),but I get below error:
no implicit conversion of Net::HTTPOK into String
How can I do this?
Net::HTTP has responses in body. so, Try
JSON.parse response.body
You have to pass the response.body as the parameters into the JSON.parse method then assign the method to a variable to use elsewhere in your code.
parse_res = JSON.parse(res.body)

RestResponse is null on POST request using RestSharp

I am making a POST request with RestSharp (on windows phone 7.1 client). I sent string to a service in a request body. Looks like the service is successfully called and it returns proper value (integer), however response object is null:
client.ExecuteAsync<T>(request, (response) => {
data = response.Data; // response is null in debugger
});
I cant understand why is that so.
<T> isn't a valid value for that call. I'm not sure that would even build there unless you've wrapped it in a generic method.
Also, is the response coming back as plain text? What's the Content-Type returned? Most likely, you should just use ExecuteAsync(request, callback) without the generic parameter and grab the data out of response.Content which is a string of the response body. response.Data is for the automatically deserialized XML or JSON (or custom) response if you use the generic method overload that specifies a type to deserialize to.
This seems to be an ongoing issue with RestSharp asynchronous calls - for HTTP transport errors ErrorException object is useless (returns null). Check the StatusCode property if it returns with anything but HttpStatusCode.OK. StatusDescription is not extremely useful either as it doesn't match complete status message from server response payload.

Resources