Reputation: 4080
Backend developer gave me API description and that uses GET method and it is JSON format.
I never tried this way and as far as I know it's not possible that sending data in request body. with GET method in retrofit library.
He uses Django. And I tried with Query and Path... And nothing works... Even I tried with no annotation with the parameter.
{
"data": "oicudsfqoerzxddadsdf"
}
1.
@GET("find/data")
fun findData(
@Header("Authorization") sessionId: String,
@Query("data") data: String
): Call<FinderResult>
2.
@GET("find/data")
fun findData(
@Header("Authorization") sessionId: String,
data: String
): Call<FinderResult>
3.
@GET("find/data")
fun findData(
@Header("Authorization") sessionId: String,
dataObj: DataObj
): Call<FinderResult>
@Keep
class DataObj(var data: String){
}
All didn't work. However, It worked on Postman using raw format(should select JSON). How can I use GET request with JSON? what's the problem?
Upvotes: 0
Views: 676
Reputation: 602
GET method purpose is only to retrieve data from server, not to send data. the only way to send data is using of query parameter in url which it's limitation is 2000 char.
When we want to use query parameter for sending data purpose we should be careful to send well-formed url characters. JSON need to be processed before attaching to URL.
So my advice is using @Query("<name of parameter which is specified by server>")
and putting @FormUrlEncoded
over findData
method.
@FormUrlEncoded
@GET("find/data")
fun findData(
@Header("Authorization") sessionId: String,
@Query("<name of parameter which is specified by server>") data: String
): Call<FinderResult>
For more information look at:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded https://www.vogella.com/tutorials/Retrofit/article.html
Upvotes: 1