How to pass Query String Parameters in GET url using Rest Assured?
URL is: http://example.com/building
My Query Strings are :
globalDates:{"startMs":1473672973818,"endMs":1481448973817,"period":90}
limitTo:6
loadTvData:true
startFrom:0
userId:5834fb36981baacb6a876427
You can pass them as a queryParam ..
given()
.queryParam("globalDates", "{\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90}")
.queryParam("startFrom", "0").queryParam("limitTo", "6").queryParam("loadTvData", true)
.queryParam("startFrom", "0").queryParam("userId", "5834fb36981baacb6a876427")
.when().get("http://example.com/building"). ...
And also you can put this queryparams in Map like follows,
HashMap<String, String> params = new HashMap<String, String>() {{
put("globalDates", "{\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90}");
put("limitTo","6" );
,...
}}
And post it like follows,
resp = RestAssured.given()
.headers(headers)
.queryParameters(params)
.post(apiURL).andReturn();
QueryParam can be passed as:
String endpoint = "http://example.com/building";
var response = given()
.queryParam("globalDates", "{\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90}")
.queryParam("startFrom", "0").queryParam("limitTo", "6").queryParam("loadTvData", true)
.queryParam("startFrom", "0").queryParam("userId", "5834fb36981baacb6a876427")
.when().get(endpoint).then();
For our api with a type like this:
https://my.api.com/meeting?page=0&size=1
We have:
Response response = requestSpecification.queryParam("page", 0).queryParam("size", 1).get(baseEndpoint);
You can pass query string parameters in a GET URL using Rest Assured like this :
when()
.parameter("globalDates","startMs","1474260058054","endMs","1482036058051","period","90")
.parameters("limitTo","6")
.parameters("loadTvData","true")
.parameters("startFrom","0")
.parameters("userId","5834fb36981baacb6a876427");
Related
In akka-http, how do we extract a list of query parameters of varying length from incoming request?
Request url can be like this:
.../employees?city=london,ny,paris
Number of cities may vary with every request.
From your solution, you can replace the Symbol part like
parameters("city".repeated)
See the akka doc
If you want to keep your value as a comma-separated list of values, you can create a custom directive like
def paramAsList(key: String): Directive1[List[String]] =
parameter(key)
.map(x => x.split(",").toList)
...
get {
paramAsList("city") => cities {
....
With this, your url .../employees?city=london,ny,paris should work
Got it working as:
path("searchByCity") {
get {
parameters(Symbol("city").*) {cities =>
.....
}
}
}
URL is now as:
.../employees?city=london&city=ny&city=paris
function getQueryParams(url){
let urlParts = url.split('?');
if(urlsParts?.length > 1){
let params = urlParts[1].split('&');
return params
}
return null
}
var queryParams = getQueryParams('.../employees?city=london,ny,paris')
I have a method in my api Controller which have the following signature
[HttpGet]
[Route("api/Controller/GetResult")]
public ApiResult<List<IDocument>> GetResult(DateTime date, Guid id, List<Guid> Ids)
I need to call it using PostMan but I don't know how to send the last argument which is a list of Guid in the parameter list, any help?
you can replace all params with JObject as following :
pass JSON:
{"date":"2019-02-17", "id":"00000000-0000-0000-0000-000000000001", "Ids":["00000000-0000-0000-0000-111111111111", "00000000-0000-0000-0000-222222222222"]}
ACTION:
public ApiResult<List<IDocument>> GetResult(JObject json)
{
DateTime date = json.FirstOrDefault(x => x.Name == "date"))?.Value.ToString(); // simple parameter
//----> output: "2019-02-17"
Guid id = new Guid(json.FirstOrDefault(x => x.Name == "id"))?.Value.ToString()); // simple parameter
//----> output: "00000000-0000-0000-0000-000000000001"
List<JToken> Ids = json.FirstOrDefault(x => x.Name = "Ids")?.Value.ToList(); // list
//----> output: ["00000000-0000-0000-0000-111111111111", "00000000-0000-0000-0000-222222222222"]
//you should specify the type of "JToken" values as this:
List<Guid> IdsList = new List<Guid>();
foreach (var Id in Ids)
{
IdsList.Add(new Guid(Id.Value<string>()));
}
}
You can use postman in-build Dynamic Variable $guid .
This generates a v4 style guid at runtime.
Try this GET request using Postman:
https://httpbin.org/anything?IDs=["{{$guid}}","{{$guid}}","{{$guid}}"]
I have this ionic service but when i pass the parameters in set Queryparametrs function it wont work.
var sample = function(title,description,adress,country,userid)
{
var req = new WLResourceRequest("/adapters/eventAdapter/addEvent", WLResourceRequest.POST);
req.setQueryParameters("params", "['"+title+","+description+","+adress+","+country+","+userid+"']");
return req.send().then(function(res) {
........
}, function(bad) {
.......
});
}
any help ?
If your content (title, description, etc) contains any invalid json characters, it could be that the string you generated is invalid.
As you guessed correctly, using JSON.stringify is the safer option.
var params = [title, description, adress, country, state, userid];
req.setQueryParameters("params",JSON.stringify(params));
Also, your request is using POST, and so it is expected to use form parameters instead of query parameters:
req.sendFormParameters({"params":JSON.stringify(params)})
I want to send string parameters in Leanplum api using action script
Eg param:{"Element":"Hi"}
var request:URLRequest = new URLRequest("https://www.leanplum.com/api");
request.method = URLRequestMethod.GET;
var variables:URLVariables = urlVariables;
variables.userId = userId;
variables.event = eventName;
var params:Object = new Object();
params.Element = "Hi";
var paramStr:String = JSON.stringify(params);
variables.params = paramStr;
variables.appId = appId;
variables.clientKey = clientKeyProduction;
variables.apiVersion = apiVersion;
variables.action = "track";
variables.versionName = AppInfo.getInstance().appVersion;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
trace(e.target.data);
});
loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void {
trace(e.target.data);
});
loader.load(request);
This is actual Request (App ID and ClientKey are dummy):
https://www.leanplum.com/api?clientKey=V42fKaJaBuE&userId=1010¶ms={"Element":"Ur"}&appId=HEVdDlXiBVLwk&event=Element_Opened&action=track&versionName=2.3.0&apiVersion=1.0.6&info=Lu
Encoded Request:
https://www.leanplum.com%2Fapi%3FclientKey%3DV42fKaJaBuE%26userId%3D1010%26params%3D%7B%22Element%22%3A%22Ur%22
%7D%26appId%3DHEVdDlXiBVLwk%26event%3DElement_Opened%26action%3Dtrack%26versionName%3D2.3.0%26apiVersion%3D1.0.6%26info%3DLu
if I run above request in any rest client I get the same status success : true .
I am getting the response {"response": [{"success": true}]} but I can't find the parameters with value string in Leanplum dashboard, its listing parameter name but not the String Value for parameter.
If you apply some combination of filters then you can see values of parameter you sent to leanplum. like First select the occurrence of some event then apply Group by parameter then select parameter you want to see the data for.
Its a little different from flurry, Google analytics etc.
I'm trying to pass a string with special characters to your web api but is giving error.
Below the line where I pass the values pro web api:
string listaParcelaSeparadoVirgula = null;
foreach (var item in listaParcelas)
{
listaParcelaSeparadoVirgula = listaParcelaSeparadoVirgula + ";;" + item;
}
var result = HttpUtility.UrlEncode(listaParcelaSeparadoVirgula);
var response = client.PostAsJsonAsync("api/LancamentoReceitaDespesa/AddLancamentoParcelar/" + result, lancamentoReceitaDespesa).Result;
the result is a string variable with values separated by ";;". Below the contents of the string:
";;aaaaa 1/2||10/01/2014|100,00||;;aaaaa 2/2||10/02/2014|100,00||"
with UrlEncode:
"%3b%3baaaaa+1%2f2%7c%7c10%2f01%2f2014%7c100%2c00%7c%7c%3b%3baaaaa+2%2f2%7c%7c10%2f02%2f2014%7c100%2c00%7c%7c"
Error:
{"Error while copying content to a stream."}
How can I pass these values pro web api?
Well you could try encode value with base64 since in url you could have special symbols
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(result);
var response = client.PostAsJsonAsync("api/LancamentoReceitaDespesa/AddLancamentoParcelar/" + System.Convert.ToBase64String(plainTextBytes), lancamentoReceitaDespesa).Result;
then in web
public void AddLancamentoParcelar(string base64EncodedData) {
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var result = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
I am not sure if its the best solution but as you could have any symbol in url then its could be an solution.