2

I'm trying to put id this way:

@Headers("Content-type: multipart/form-data")
@POST("/files/upload?dir=userIcons/{id}")
fun upload(@Query("id", encoded = true) id: String,
           @Body encodedImg: String) : Call<IconModel>

But I getting error with replacing. And idk how to fix it.

Here is error message:

java.lang.IllegalArgumentException: URL query string "dir=userIcons/{id}" must not have replace block. For dynamic query parameters use @Query.
John
  • 355
  • 2
  • 4
  • 15
  • 1
    Try `@Path` instead of `@Query` and see if the replace block works. If not, you may need to remove `?dir=userIcons/{id}` from the URL, and have `@Query` provide the full `dir` value (rather than just the `id` portion). – CommonsWare Apr 28 '18 at 11:03
  • @POST("/files/upload?dir=userIcons/{id}") fun upload(@Path("id") id: String) : Call – NSimon Apr 28 '18 at 11:05
  • you can provide the full URL **concat the url and send** ..see https://stackoverflow.com/questions/49293354/how-to-pass-get-parameters-to-retrofit-request/49293492#49293492 – Santanu Sur Apr 28 '18 at 11:07

1 Answers1

2

Since you can't have a replace block in a query (regardless of how you then try to substitute it, using @Path won't work with it either), here's the way to do it instead. You can remove the query parts from the path passed to the @POST annotation:

@Headers("Content-type: multipart/form-data")
@POST("/files/upload")
fun upload(@Query("dir", encoded = true) id: String,
           @Body encodedImg: String): Call<IconModel>

And then you have to pass in the entire value of the query parameter when you call the API:

val call = api.upload("userIcons/foo", "bar")

This will result in a call like this, which I believe is what you want:

/files/upload?dir=userIcons/foo 
zsmb13
  • 85,752
  • 11
  • 221
  • 226