Not getting top 1 element when caling API of SAP Successfactor using RestAssured - rest-assured

I'm trying to call SAP SF API calls using RestAssured in my application. The service is given parameter as top 1 .
RestAssured.baseURI="https://sandbox.api.sap.com/successfactors/odata/v2/PerGlobalInfoCHN?%24top=1";
RequestSpecification httpPost = RestAssured.given().headers("ApiKey","*key*");
Response response= httpPost.request(Method.GET);
String responseBody = response.getBody().asString();
System.out.println("Response Body is :"+response.getBody().prettyPrint());
The output should be only one top element but I'm getting all the results.
When same URL is given in postman , it returns top 1 only.
What is the issue here? Do we have to provide top=1 as a parameter in header?

Related

How to make GET Request with api key in LUA

I am trying to get a request from Google Sheet API, but I don't know how to pass API key in code.
local http = require("socket.http")
local body, code, headers, status = http.request("https://sheets.googleapis.com/v4/spreadsheets/1AQK1WHGsavVmhNugAipMsrweB3m25xp01vtzGA8BvwE/values/Global!A1:D5")
print(code, status, body)
right now I'm getting Error 403
Add it at the end of the URL as a query parameter, like this:
http.request("https://sheets.googleapis.com/v4/spreadsheets/[spreadsheet-id]/values/Global!A1:D5?key=[YOUR_API_KEY]")
For next time, you could check how Sheets API [1] makes a request by clicking in the expand icon on "Try this API" window after you make a request from there.
[1] https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get

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

restsharp and Postman

I am attempting to get an OAuth2 access token from ZOHO using RestSharp code. The Postman simulation works correctly so I know there is something I'm missing in my code.
I always get an "invalid client id" result status. However in Postman, it works and returns a code when I click the "Get new access token". I have the same items as in the Postman authorization tab (client_id, client_secret, etc). In Postman, "Body" is set to "none", and there are no parameters or headers. The only difference between my code and postman, is that Postman requires the Callback URL. My code is trying to get the code using "self-client", which bypasses the callback URL.
I have tried several different alternatives to the request call including ParameterType.Body, and ParameterType.GetOrPost. Is GetOrPost the same as a form?
client = New RestClient(ZOHO_API_URL)
request = New RestRequest(TokenUrl, Method.POST)
request.AddHeader("content-type", "application/x-www-form-urlencoded") ' also tried: "application/json")
request.AddParameter("grant_type", "authorization_code",
ParameterType.GetOrPost)
request.AddParameter("client_id", Client_ID, ParameterType.GetOrPost)
request.AddParameter("client_secret", Client_Secret,
ParameterType.GetOrPost)
request.AddParameter("code", Grant_Token, ParameterType.GetOrPost)
response = client.Execute(request)
This is the translated Postman code for RestSharp:
var client = new RestClient("http://");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);
Any ideas on what I am doing wrong. I have tried to view the raw data coming across with Fiddler, but when I do that, Postman indicates a failure.
What code do I need to use to duplicate what Postman is doing?
How do I implement a callback URL if that is also required?
I quickly checked ZoHo REST API docs and it seems like you should use the Limited Input Device authentication flow.
From what I can understand from their help page, you indeed need to do a POST request, but parameters must be specified as query parameters:
https://accounts.zoho.com/oauth/v3/device/code?
client_id=1000.GMB0YULZHJK411248S8I5GZ4CHUEX0&
scope=AaaServer.profile.READ&
grant_type=device_request
You will also get better off with JSON set as a default for serialisation and content type just by using client.UseJson().
It maybe that Postman is following a redirect from your API endpoint as the functionality is different Postman verses RestSharp (possibly missing a trailing slash or similar).
Try adding
client.FollowRedirects = false;
to your RestSharp code and analyse the result.

Swagger UI showing response body {} and status 0

I am using swagger to create API docs.
My issue is that once we send a call to the API, the browser sends an OPTIONS call and it returns "OK"; after that the browser sends the actual call of API and then I am not receiving the expected output.
Instead of it, it's showing no-content in response and 0 as the response code.
Please can any one help.

Setting content type/ encoding in Jersey REST Client

HI I have been trying to call REST POST API using jersey REST Client. The API is docs is
URL:
METHOD: POST
Header Info:-
X-GWS-APP-NAME: XYZ
Accept: application/json or application/xml
My Sample Jersey client code is
Client client = Client.create();
WebResource resource=client.resource(URL);
resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML);
resource.type(javax.ws.rs.core.MediaType.APPLICATION_XML);
resource.type("charset=utf-8");
ClientResponse response = resource.post(ClientResponse.class,myReqObj);
I have been trying this code variation since last 1 week and it is not working. Any help in this regard is highly appreciated.
The tricky part is that the WebResource methods follows the Builder design pattern so it returns a Builder object which you need to preserve and carry on as you call further methods to set the full context of the request.
When you do resource.accept, it returns something you don't store, so it's lost when you do resource.type and therefore only your last call takes effect.
You'd typically set all the criterias in one line, but you could also save the output in a local variable.
ClientResponse response = client.resource(URL)
.accept(MediaType.APPLICATION_XML)
.type(MediaType.APPLICATION_XML)
.post(ClientResponse.class,myReqObj);
I do like that.
Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(a, "application/json; charset=UTF-8"));
here, 'a' is account class instance which like
#XmlRootElement
public class account {
...
...
}

Resources