简体   繁体   中英

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

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM