Adding a query to a URL that already has a question mark with Retrofit - kotlin

By good design, Retrofit will add your query to your URL as an ampersand rather than a question mark if there's already a question mark in the URL. However, I have no control over the URL and so am unable to change it. Is there a way that I can override #Query so that it will use a ? instead?
interface Service {
#GET("/php/project/?sign.externalGetToken")
fun getAuthToken(
#Query("site") site: String,
#Query("time") time: String,
#Query("secretkey") secretKey: String
): Call<TokenResponse>
}
Printed URL:
http://baseurl.com/php/project/?sign.get&site=my_site&time=1538755984978&secretkey=site_secret_key
I need that URL to be:
http://baseurl.com/php/project/?sign.get?site=my_site&time=1538755984978&secretkey=site_secret_key
Note the double ? in ?sign.get?site.

Is an url like this acceptable ?
http://baseurl.com/php/project/?sign.get?&site=my_site&time=1538755984978&secretkey=site_secret_key
There is an & just after the second question mark, but in theory the server should still be able to read it. If yes you can do it simply like this:
#GET("/php/project/?sign.externalGetToken?")
Or you can also try like this:
#GET("/php/project/?sign.externalGetToken")
fun getAuthToken(
#Query(value = "?site", encoded = true) site: String, // encoded ensure that retrofit doesn't encode the ? into '%3F'
#Query("time") time: String,
#Query("secretkey") secretKey: String
): Call<TokenResponse>
which will result in
http://baseurl.com/php/project/?sign.get&?site=my_site&time=1538755984978&secretkey=site_secret_key
the & will this time be before the question mark, but not sure if it will work. Also this second solution requires you never pass null for the site parameter of your call, else the "?site" will simply not be printed.
As a last resort, if nothing of the above works, you can try to use instead the #Url annotation from retrofit. It seems a little in contradiction with the use of a service interface if you ask me, but at least it should work.
Just replace your declaration by this:
#GET
fun getAuthToken(
#Url url: String,
#Query("time") time: String,
#Query("secretkey") secretKey: String
): Call<TokenResponse>
then just call it like this
service.getAuthToken("/php/project/?sign.externalGetToken?site="+ yourSite, yourTimeString, yourSecretKey");
and you will have the url you want. But it requires to always send the url as a parameter.

Related

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"]

Multi param values are not replaced properly in the path

I have multiple parameters in the REST call as shown below
/integration/live/rest/accessProfile?page=0&pageSize=10&sortBy=name&fieldList=name,id,date_created,date_modified,created_id,modified_id&filter=id%20not%20equal%20to%20%27200%27%20AND%20id%20not%20equal%20to%20%27100%27%20AND%20id%20not%20equal%20to%20%27101%27%20AND%20id%20not%20equal%20to%20%27102%27%20AND%20id%20not%20equal%20to%20%27103%27%20&getTotalRecordCo
My code is
* params { page: '0', pageSize: '10',sortBy: 'name', fieldList: ['name','id', 'date_created', 'date_modified', 'created_id', 'modified_id'],filter: 'id%20not%20equal%20to%20%27200%27%20AND%20id%20not%20equal%20to%20%27100%27%20AND%20id%20not%20equal%20to%20%27101%27%20AND%20id%20not%20equal%20to%20%27102%27%20AND%20id%20not%20equal%20to%20%27103%27%20',getTotalRecordCount:true }
And path '/integration/live/rest/accessProfile'
When I am running the test cases the path is not properly replaced in the REST call
After running the actual call sent to the server is
https://vm-trunk-wmic-01.eur.ad.sag/integration/live/rest/accessProfile?page=0&pageSize=10&sortBy=name&fieldList=name&fieldList=id&fieldList=date_created&fieldList=date_modified&fieldList=created_id&fieldList=modified_id&filter=id%2520not%2520equal%2520to%2520%2527200%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527100%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527101%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527102%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527103%2527%2520&getTotalRecordCount=true
All the params are replaced properly except for 'fieldList' parameter in the path.
I am looking for correct syntax to pass my below REST call
/integration/live/rest/accessProfile?page=0&pageSize=10&sortBy=name&fieldList=name,id,date_created,date_modified,created_id,modified_id&filter=id%20not%20equal%20to%20%27200%27%20AND%20id%20not%20equal%20to%20%27100%27%20AND%20id%20not%20equal%20to%20%27101%27%20AND%20id%20not%20equal%20to%20%27102%27%20AND%20id%20not%20equal%20to%20%27103%27%20&getTotalRecordCount=true
Try:
* params { fieldList: 'name,id,date_created,date_modified,created_id,modified_id' }
EDIT: please note that commas will be URL-encoded as per the HTML spec. If you really want to "see" the commas, build the url yourself.
For an example of the 2 ways to do this, see this commit: https://github.com/intuit/karate/commit/14c6321606bb6bcb626614248f85cc8ea50c61b6

m.request: use URL which contains colons

I have a m.request call like this:
mCmdName = "cn:cmd:list:deselectAll";
m.request({
method : "POST",
url : "/testing/cmd/" + mCmdName,
data: data
});
Now m.request calls
xhrOptions.url = parameterizeUrl(xhrOptions.url, xhrOptions.data);
and tries to replace all ':[name]' parts with data[name] which results in 'undefined' as data doesn't contain any of the keys. Data is just the data object of the XHR request.
Is there a way to prohibit this default behavior?
Thanks, Stefan
PS: I'm asking here and not in mithril mailing list because I can't post there for incomprehensible reasons. Maybe somebody can give me a hint on this.
Have you tried
encodeURIComponent("cn:cmd:list:deselectAll")
which gives you
cn%3Acmd%3Alist%3AdeselectAll
If necessary you can decode on the server.

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

QUnit and urlencode

I'm trying to test a utility method I have that creates urlencoded query strings. It somehow decodes "expected" in to: ?foo=foo val&bar=bar&val ... so it's decoding the urlencoding!
test("test make_params properly url encodes", function() {
var o = {"foo":'foo val',"bar":'bar&val'};
var actual = make_params(o);
equals('?foo=foo+val&bar=bar%26val', actual, "Expected urlencoded string built to be" + '?foo=foo+val&bar=bar%26val');
});
Results in:
1. Expected urlencoded string built to be?foo=foo+val&bar=bar%26val, expected:
"?foo=foo val&bar=bar&val" result: "?foo=foo+val&bar=bar%26val", diff: "?foo=foo val&bar=bar&val" "?foo=foo+val&bar=bar%26val"
Is this a bug in qunit or am I overlooking something?
One minor issue: equals expect the actual value as the first argument, the expected as the second. And equals is now deprecated in favor of equal.
Based on that its likely that the test works fine, but the make_params method doesn't actually encode anything.