Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @Field and @Query in retrofit

In some POST requests, I don't know when to use @Field with FormUrlEncoded and when to use @Query

For Example:

@POST("list-products-for-sale")
        Call<ListAllProductsResponse> getNewProducts(@HeaderMap Map<String, 
        String> headers,@Query("lastProductId") String lastProductId);

When I tried to use @Field here it was not responding properly and when I switched it to @Query it's working great.
I want to know why @Field isn't working while Query can work perfectly and I did tested in POSTMAN where I sent the data as a formurlencoded and it's giving me the results fin.
EDIT
BTW I'm passing Content-Type:application/json, Accept: application/json with an Authorization key

like image 990
Arun Sriramula Avatar asked Sep 09 '19 04:09

Arun Sriramula


Video Answer


1 Answers

@Field is used to send @FormUrlEncoded request in Retrofit which hides your parameter and not attach with url to provide security.Used for POST request.

@Query parameter appended to the URL.

If you are using @Field request than it will hides your parameter and not append with the url.

If you are using @Query request than all your parameter is append to your request and visible to users.

Depend on your api request you have to use one of above annotation. If the api request accept the @FormUrlEncoded data than use @Field or if they want to attached it with url than use @Query.

You can get more information from below link :

1) https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html

2) https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/Field.html

like image 124
Mahavir Jain Avatar answered Oct 17 '22 03:10

Mahavir Jain