Nihthiya Althaf
Nihthiya Althaf

Reputation: 201

How to pass data in url without Query in Retrofit

I am using Retrofit for API calls.. My API is like this https://test/update/123456789..

In this API need to pass "123456789" as value.. Can anyone help me to do this using Retrofit.. How can I pass this value w/o Query..

Thanks in Advance..:)

fun updatePriceTesting(@Query("device_id") deviceId: String, @Query("appId") appId: String, @Body RequestBody: RequestBody) : Deferred

Response>

Upvotes: 0

Views: 1530

Answers (2)

Mohammed Alaa
Mohammed Alaa

Reputation: 3320

As it's appears that this is not clear I will write it as answer

if your url will be like this update/123456789?appId=5452266 which is update/device_id?appId=5452266 then your function call will be

@FormUrlEncoded
@POST("update/{device_id}")
fun updatePriceTesting(@Path("device_id") deviceId: String, @Field("appId") appId: String, @Body RequestBody: RequestBody) : Deferred

if your url will be like this update/5452266?device_id=123456789 which is update/appId?device_id=123456789 then your function call will be

@FormUrlEncoded
@POST("update/{appId}")
fun updatePriceTesting(@Field("device_id") deviceId: String, @Path("appId") appId: String, @Body RequestBody: RequestBody) : Deferred

if your url will be like this update/123456789/5452266 which is update/device_id/appId then your function call will be

@FormUrlEncoded
@POST("update/{device_id}/{appId}")
fun updatePriceTesting(@Path("device_id") deviceId: String, @Path("appId") appId: String, @Body RequestBody: RequestBody) : Deferred

Upvotes: 0

Mustafa Ibrahim
Mustafa Ibrahim

Reputation: 606

To pass a value in retrofit you have to annotate it as @Query,or @Path,in case you dont want to use @Query,you can use @Path in the method parameter ,for example : if you want to send a value to your api , let us say www.fb.com/post,to pass id of post to retrieve,we have to do it like that:

@Get("www.fb.com/post/{post_id}")

if your id is an int then you can use :

Call<Post> getPost( @Path("post_id") int postId);

but if it was an string :

Call<Post> getPost( @Path("post_id") String postId);

and then you can call this method :

yourApiServiceObj.getPost(thePostId);

Upvotes: 2

Related Questions