java - Default/Constant values for POST/PUT arguments with Retrofit -
using retrofit rest client library square, there anyway of providing default/constant values post/put fields in call.
i know including constant query parameters including them in path, work body parameters.
i have api looks similar to:
post /api/create type=constanta&value={value} post /api/create type=constantb&value={value}&othervalue={othervalue} where second variant requires additional argument supplied. rather having single java method took 3 arguments, hoping able elide constants method call, like:
create(string value); create(string value, string othervalue); and have retrofit inject type argument constant.
given adding @formurlencoded can added modify how body encoded, if it's not natively supported retrofit, there anyway of adding own annotation , injecting such default values? ( doesn't appear requestinterceptor allows 1 modify body.. ).
maybe 1 option send object, encapsulates values, instead of string values separately? object implement default values.
for example, create class:
public class createobject { private string type = "constant"; private string value; private string othervalue; public createobject(string value, string othervalue) { this.value = value; this.othervalue = othervalue; } } your class handles constant. set default value "constant", did above, or set on fly in constructor.
now you've create object values , make request retrofit. instead of using string values directly, pass object. interface this:
public interface createservice { @post("/api/create") void create(@body createobject create, callback<createobject> cb); } the request implementation this:
createobject create = new createobject("value", "othervalue"); createservice.create(create, new callback<createobject)() {…}); this should include 3 of values in request body, if set. if value null, won't included in request body. based on 2 examples above, need 1 interface method. values sent based on createobject pass on. example, if set othervalue null, won't part of request body.
my examples modified from: https://futurestud.io/blog/retrofit-send-objects-in-request-body/
Comments
Post a Comment