Reputation: 397
Trying to reach an custom method on my server using an android client create a bad request error.
Here is the custom method on the server (tested with strongloop explorer):
Question.remoteMethod(
'top', {
http: {path: '/top', verb: 'get'},
accepts: [
{arg : 'start', type: 'number'},
{arg: 'pagination', type: 'number'}
],
returns: {arg: 'questions', type: 'array'},
description: ['Returns an array obj the latest added questions']
}
);
Code in the android Question repository :
@Override
public RestContract createContract() {
RestContract contract = super.createContract();
contract.addItem(
new RestContractItem("/" + getNameForRestUrl() + "/top?" +
"start=" + ":start" + "&pagination=" + ":pagination", "GET"),
getClassName() + ".top");
return contract;
}
public void top(int start, int pagination, ListCallback<Question> cb) {
Map<String, Integer> params = new HashMap<String, Integer>();
params.put("start", start);
params.put("pagination", pagination);
invokeStaticMethod("top", params,
new JsonArrayParser<Question>(this, cb));
}
When I use this following code to test the created request url :
RestContract contract = this.createContract();
Map<String, Integer> params = new HashMap<String, Integer>();
params.put("start", 0);
params.put("pagination", 2);
String t = contract.getUrl("/" + getNameForRestUrl() + "/top?" +
"start=" + ":start" + "&pagination=" + ":pagination", params);
t is: "/Questions/top?start=0&pagination=2" which is the right url (according to strongloop explorer). Still, the use of the fucntion TOP returns a bad request error.
Do you have any idea why I got an error, and how I should modify the function to get the results ?
Upvotes: 2
Views: 244
Reputation: 397
Finally got the answer. The error was within the Question Repository.
Here is the right peace of code :
@Override
public RestContract createContract() {
RestContract contract = super.createContract();
contract.addItem(
new RestContractItem("/" + getNameForRestUrl() + "/top", "GET"),
getClassName() + ".top");
}
public void top(int start, int pagination, ListCallback<Question> cb) {
Map<String, Integer> params = new HashMap<String, Integer>();
params.put("start", start);
params.put("pagination", pagination);
invokeStaticMethod("top", params,
new JsonResponseParser<Question>(this, cb, "questions"));
}
You must not specify filter when you create the rest contract as the using the top method below will automaticaly add the parameters given to the parameters Map as a filter with the form : http://serverip:port/api/Questions/top?filter=value1&filter2=value2
Upvotes: 2