Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit: what is different between @Field and @Body

In some post request, I don't know when to use @Field, when to use @Body. Like whats the difference between:

@POST("users/register")     Call<String> register(@Body RequestBody registerRequest); 

and:

@POST("users/register")     Call<String> register(@Field String id, @Field String pass); 

Can I use @Body instead of @Field, and reverse ? If not, why ? And how to know this case use @Body, other case use @Field ?

Can you please give me some case and explain, thank you.

like image 634
Cuong Nguyen Avatar asked Apr 11 '17 04:04

Cuong Nguyen


People also ask

What is @part in retrofit?

One part consists of a header and a body. The body can be any type of media and can contain text or binary data. A multipart media type can be included in a part. Retrofit supports requests that are in parts. In this tutorial, we will make a basic multipart request to a Ktor backend that accepts user profiles.

What is field in retrofit Android?

Annotation Type FieldNamed pair for a form-encoded request. Values are converted to strings using Retrofit. stringConverter(Type, Annotation[]) (or Object. toString() , if no matching string converter is installed) and then form URL encoded. null values are ignored.

What is @streaming in retrofit?

The interface for Retrofit REST service, in this case, the GET, the @Streaming enables the downloading for large file. public interface RetrofitInterface { @Streaming @GET Call<ResponseBody> downloadFileByUrl(@Url String fileUrl); }

What is the advantage of retrofit?

Advantages of retrofitIt is easy to use and understand. It supports request cancellation. It supports post requests and multipart uploads. It supports both synchronous and asynchronous network requests.


2 Answers

@Body – Sends Java objects as request body.

@Field – send data as form-urlencoded. This requires a @FormUrlEncoded annotation attached with the method. The @Field parameter works only with a POST. @Field requires a mandatory parameter. In cases when @Field is optional, we can use @Query instead and pass a null value.

like image 173
Fathima km Avatar answered Sep 23 '22 08:09

Fathima km


Both are used for posting data only, but they have following difference -

The @Body annotation defines a single request body.

    interface Foo {        @POST("/jayson")        FooResponse postJson(@Body FooRequest body);     } 

That means if you are using @Body, it should be only parameter. It is helpful when you have already a JsonObject and you want to send it as it with you api call.

Another way is, you can send data using @Field and send the Place object as a JSON string.

@POST("/post/addphoto/")     public void addImage(@Field("image_url") String url, @Field("caption") String caption, @Field("google_place_id") String placeId, @Field("facebook_place") String place, Callback<UploadCallBack> response); 

Hope it will help... :-)

like image 31
Neo Avatar answered Sep 24 '22 08:09

Neo