Tomasz Dubiel
Tomasz Dubiel

Reputation: 3

Parameter in REST method is not properly handled in Retrofit

I'm working on Android app which connects to REST and invokes a method. This is Embarcadero REST DataSnap. Using parameters like "@Query" is good when you invoke method like that:

www.app.net/api/searchtypes/862189/filters?Type=6&SearchText=School

However, here methods are invoked differently:

/datasnap/rest/some_class/some_method/some_parameter

Below is simple class to handle parameter which goes in request body:

public class Dane {
    private int NAGL;

    public Dane(int NAGL) {
        this.NAGL = NAGL;
    }
}

When I try to use Retrofit annotation @Query, for example:

 @POST("datasnap/rest/TstBaseMethods/%22SelectSQL%22/{aSelectSQL}")   
Call<Logowanie> selectSQL(@Header("Authorization") String credentials,@Body Dane json,@Query("aSelectSQL") String aSelectSQL);

String dane = Credentials.basic("admin","admin");  
Dane json = new Dane(11101); 


Call<Logowanie> sql = gerritAPI.selectSQL(dane,json,"select n.datadok from nagl n where n.id_nagl =:NAGL");

and I launch the app, I see in logs

TstBaseMethods.SelectSQL: {aSelectSQL} << {"NAGL":11101}

The content of aSelectSQL is not sent to the server. I've already noticed that if I hardcode the content into URL and I invoke this as below, it works:

@POST("datasnap/rest/TstBaseMethods/%22SelectSQL%22/select%20n.datadok%20from%20nagl%20n%20where%20n.id_nagl%3D%3Anagl")
 Call<Logowanie> selectSQL(@Header("Authorization") String credentials,@Body Dane json);  

Is there any way to pass properly content of the parameter to the server? It won't be good to hardcode all parameters in URL.

Upvotes: 0

Views: 52

Answers (1)

Macmist
Macmist

Reputation: 723

So, in retrofit, the @Query annotation is used for query parameter. It will add your parameter as a query parameter, for example:

@GET("/api/somePath")
Call<JSONObject> getSomething(@Query("foo") String foo);
...
service.getSomething("bar")

Will actually result in the url:

https://yoursite.com/api/somePath?foo=bar

Here, in your case, you are using {} inside the url, which indicates retrofit that you are adding a path parameter. So your post should be like this

@POST("datasnap/rest/TstBaseMethods/%22SelectSQL%22/{aSelectSQL}") 
Call<Logowanie> selectSQL(@Header("Authorization") String credentials,@Body Dane json,@Path("aSelectSQL") String aSelectSQL);

Upvotes: 1

Related Questions