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
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With