Is it possible to get the values of the method parameters from the Context() in Play! 2? - playframework-2.1

Using Play!Framework 2.1.3, is there a way to get the value used in the URL from the matched routes?
An example to be more precise:
Routes:
GET /users/:name/:page controllers.Users.list(name: String, page: Integer)
and this URL:
/users/ben/1
Is it possible, from the Context().current(), to get the value ben and 1 without having to parse the Context().current().path() ?

Related

how to pass multiple type of data to API from server side as formdata?

I want to create circuit (messenger like skype) conversation from my server side code (C#) by calling circuit api.
Function : https://circuitsandbox.net/rest/v2/conversations/
I have to pass two type of parameters :
1) Participants - string array
2) topic - string
According to function definition (in swagger), I have to pass them as formdata. But, when I am trying to encode the parameters
var content = new FormUrlEncodedContent(values);
It is not accepting the array string for participants list. It is expecting "key/value" pair as "string/string".
I have even tried to create the JSON serialization also
JsonConvert.SerializeObject(values)
But the API definition is not accepting this converted values as it is expecting formdata in string/string as key/value.
I had even tried to concatenate the participants list with ";" as delimiter. But in that case, I am getting 400 error.
I tried to convert my parameters like this also
var formData = new List>();
formData.Add(new KeyValuePair("participants", JsonConvert.SerializeObject( participants)));
formData.Add(new KeyValuePair("topic", "Testing1"));
But again, I a getting 400 error.
Here is my API call
var response = client.PostAsync("https://circuitsandbox.net/rest/v2/conversations/group", content);
Can someone provide me some code snippet to pass that this data to API?
Let me emphasis, I am trying from server side code (C#) and not jquery code.
as we checked together earlier today, make the participants value a comma separated string of emails and it should work

JMeter pass JSON response value to next request

I am using JMETER to test a web app.
First I perform a http GET request which returns a JSON array such as:
[
{
"key1":
{
"subKey":
[
9.120968,
39.255417
]
},
key2 : 1
},
{
"key1":
{
"subKey":
[
9.123852,
39.243237
]
},
key2 : 10
}
]
Basically I want to take randomly one element, take the elements of key1 and create 2 variables in JMeter that will be used for the next query (if randomly it is not possible than just the 1st element).
I tried using JSON Extractor with the following settings (the example shows a single variable case):
and in the next http GET request referencing the parameter as ${var1}.
How to set JSON Extractor to extract a value, save into a JMeter variable to be used in the next http GET request?
Correct JSON Path query would be something like:
$..key1.subKey[${__Random(0,1,)}]
You need to switch Apply to value either to Main sample only or to Main sample and sub-samples
In the above setup:
Match No: 0 - tells JMeter to get random value from key1 subkey
${__Random(0,1,)} - gets a random element from the array, i.e. 9.120968 or 39.255417
More information:
Jayway Jsonpath
API Testing With JMeter and the JSON Extractor
"JMeter variable name to use" option that you've switched on there means that you'd be examining the content of this variable INSTEAD of Sample result.
So the fix is obvious: if you intend to extract whatever you extracting from Sample result - change it back to it.
PS If you intend the opposite (process the variable content, not the sample result) - let me know please.

Can Karate generate multiple query parameters with the same name?

I need to pass multiple query parameters with the same name in a URL, but I am having problems getting it to work with Karate. In my case, the URL should look like this:
http://mytestapi.com/v1/orders?sort=order.orderNumber&sort=order.customer.name,DESC
Notice 2 query parameters named "sort". I attempted to create these query string parameters with Karate, but only the last "sort" parameter gets created in the query string. Here are the ways I tried to do this:
Given path 'v1/orders'
And param sort = 'order.orderNumber'
And param sort = 'order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/orders'
And params sort = { sort: 'order.orderNumber', sort: 'order.customer.name,DESC' }
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/order?sort=order.orderNumber&sort=order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
The first two ways provide the same query string result: ?sort=order.customer.name%2CDESC
The last example does not work because the ? get encoded, which was expected and explained in this post - Karate API Tests - Escaping '?' in the url in a feature file
It's clear that the second "sort" param is overriding the first and only one parameter is being added to the URL. I have gone through the Karate documentation, which is very good, but I have not found a way to add multiple parameters with the same name.
So, is there a way in Karate to set multiple URL query parameters with the same name?
Yes you can generate multiple query parameters with the same name in karate
All values of similar key should be provided in an array.
Given path 'v1/orders'
And params {"sort":["order.orderNumber","order.customer.name,DESC"]}
And header Authorization = authInfo.token
And method get
Then status 200
And for setting single parameter using param it will be like
And param sort = ["order.orderNumber","order.customer.name,DESC"]

Default values for query parameters

Please forgive me if my question does not make sense.
What im trying to do is to inject in values for query parameters
GET1 File
Scenario:
Given path 'search'
And param filter[id] = id (default value or variable from another feature file)
POST1 File
Scenario:
def newid = new id made by a post call
def checkid = read call(GET1) {id : newid}
like if one of my feature files creates a new id then i want to do a get call with the above scenario. therefore i need a parameter there which takes in the new id.
On the other hand if i do not have an id newly created or the test creating it is not part of the suite. i want to still be able to run the above mentioned scenario but this time it has a default value to it.
Instead of param use params. It is designed so that any keys with null values are ignored.
After the null is set on the first line below, you can make a call to another feature, and overwrite the value of criteria. If it still is null, no params will be set.
* def criteria = null
Given path 'search'
And params { filter: '#(criteria)' }
There are multiple other ways to do this, also refer to this set of examples for data-driven search params: dynamic-params.feature
The doc on conditional logic may also give you some ideas.

Play reverse routing - getting absolute url

How can I get the absolute URL in play 2.2 scala when doing the following:
val promoLink = routes.Promotions.promotionsCategory(DOCID, slug)
//routes file
GET /promotions/:DOCID:/slug controllers.Promotions.promoCat(DOCID, slug)
As it stands I get a "found : play.api.mvc.Call" type mismatch on expecting a string
thanks
I suppose your promoLink should be a String containing an URL? Your question sounds a bit unclear.
If so then you probably need this:
val promoLink = routes.Promotions.promotionsCategory(DOCID, slug).absoluteURL(false)(request)
false in the .absoluteURL(false) stands for the isSecure parameter which will give you http or https url.
If you have an implicit request in scope you may omit the last (request) part