You can add static headers to retrofit requests using @Headers({}), and specific body fields using @field in the method arguments. But I want to submit constant (non-json) name-value parameters in the body of a post request. The retrofit documentation does not mention it. I shouldn't have to use an interceptor to do this either. Is @FieldMap in the method parameters my only option ? Or is there an annotation that will permit constant Fieldmap similar to @Headers ?
Asked
Active
Viewed 858 times
0
-
see http://stackoverflow.com/a/33667739/1025379 – Hun Dec 19 '16 at 00:39
-
thanks for the response, but i need to do it without interceptors.. (it was in the original question) – angryITguy Dec 19 '16 at 01:52
-
how about this? @POST("users/new?sort=desc") – Hun Dec 21 '16 at 03:42
1 Answers
1
You can use okHttp's RequestBody as your parameter
@POST("path")
Call<ResponseBody> postWithPlainText(@Body RequestBody requestBody);
And then use it like this
String plainText = "Your constant here";
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), plainText);
Call<ResponseBody> call = service.postWithPlainText(requestBody);
Response<ResponseBody> response = call.execute();

Niko Adrianus Yuwono
- 11,012
- 8
- 42
- 64
-
So you cannot setup a static body in a post request using annotations like \\@Headers. It seems that I have to submit the body via the method parameters using \\@Body ? – angryITguy Dec 19 '16 at 01:51
-
@giulio Yes I don't think you can do that with just adding an annotation. – Niko Adrianus Yuwono Dec 19 '16 at 02:06