How do you add constant Body fields in retroift POST request?

906 views Asked by At

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 ?

1

There are 1 answers

2
Niko Adrianus Yuwono On

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();