0

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?

c-an
  • 2,036
  • 1
  • 17
  • 46
  • I think the `data` in your json should be query parameter not Body .. You do not send body in GET request ..Then It should be like `@GET("find/{data}") fun findData( @Header("Authorization") sessionId: String, @Path("data") data: String ): Call`.. – ADM Mar 11 '20 at 07:07
  • Can you please check in your response method what response.raw() is returning and error code. – Afzal Khan Mar 11 '20 at 07:10
  • Your 1st method is correct take that only – Afzal Khan Mar 11 '20 at 07:11
  • @ADM why do you use Path annotation here? Can you send json data in that way? – c-an Mar 11 '20 at 07:11
  • @AfzalKhan Well, lack of parameter. – c-an Mar 11 '20 at 07:12
  • You need to ask your backend guy, where in the get request he expects the JSON data. In theory you could have a query parameter that has the stringified JSON as its value. – Ridcully Mar 11 '20 at 07:39
  • I asked him and said put the data in the request body. As far as I know, this is against to Retrofit. like request body in GET method. What should I do..? hmm – c-an Mar 11 '20 at 07:49
  • What do you mean it doesn't work? Are there any logs to attach? – Genesis Mar 11 '20 at 07:58
  • @Genesis it dies or the API doesn't work as we expected. ref: https://stackoverflow.com/questions/978061/http-get-with-request-body – c-an Mar 11 '20 at 07:59

1 Answers1

1

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

Mahdi Rajabi
  • 472
  • 1
  • 4
  • 10