postman image with Api call format is attached below,
response is success when called through postman.
but Api response is failure when try through Code.
#GET("booking-list/{userId}")
fun getOrderHistoryList(#Path("userId") userId: Int):Observable<ResponseClass>
please help me to correcting format.
You need to change #Path to #Query like below, also usedId is a query parameter so you need to remove from path.
#GET("/booking-list")
fun getOrderHistoryList(#Query("userId") userId: Int):Observable<ResponseClass>
The query component is indicated by the first question mark ("?") character. For more information look at it Query component
Your get url should be
#GET("booking-list?userId={userId}")
Related
I'm currently trying to make an ApiInterface. I'm trying to manipulate API URL by using query. So far the URL I'm using is
https://api.themoviedb.org/3/search/movie?api_key={apiKey}&language=en-US&query={query}
The url above I'm trying to change the "query" So I created this interface:
#GET("3/search/movie?api_key=${BuildConfig.MOVIE_TOKEN}&language=en-US&")
fun getMovies(
#Query("query") query: String
): Call<SearchMovieResponse>
And it works well, but what if the case of the URL is this:
https://api.themoviedb.org/3/movie/{id}?api_key=7fad718afb38c7fe3fbe9da94e0d54e6
Where I'm trying to manipulate the {id}. Is the interface like this?
#GET("3/movie/{id}?api_key=${BuildConfig.MOVIE_TOKEN}")
fun getMovieById(
#Query("id") id: String
):Call<SearchDetailMovieResponse>
Please help me understand how to utilize the annotations to modify the API urls
To use the #Query properly in the case of the following URL:
https://api.themoviedb.org/3/movie/{id}?api_key=7fad718afb38c7fe3fbe9da94e0d54e6
You would need to write your interface as such
#GET("3/movie/{id}")
fun getMovieById(
#Path("id") id: String,
#Query("api_key") apiKey: String = BuildConfig.MOVIE_TOKEN
):Call<SearchDetailMovieResponse>
For more information refer to the documentation.
You can use #Path annotation for this.
#GET("3/movie/{id}?api_key=${BuildConfig.MOVIE_TOKEN}")
fun getMovieById(
#Path("id") id: String
):Call<SearchDetailMovieResponse>
Additional Info:
If you have parameter in middle of url and want to give dynamic value then use #Path annotation as you have id in middle of url.
e-g https://test-api/3/profile. 3 is param in middle of url in that case we use #Path
If you don't have parameter in middle of url and want to pass parameters at end of url with key value pair then better to use #Query
e-g https://test-api/profile?user_id=3 user_id is key value parameter in that case we use #Query.
Note: I am sharing as far as I understood.
I am using the retrofit library for network calls. In this, I need to pass Body in GET Method. But I am getting the error while I am passing this one. In Postman it is working while passing Body for GET Method.
#GET("http://192.168.0.141:3000/api/contacts/{page_num}/{limit}")
fun getAllContacts(#Path("page_num") page_num:Int,#Path("limit") limit:Int,#Body reqBody:ContactsInpRequest):Call<AllContactsDataResponse>
I am calling get method by passing body. But I am getting the below exception.
java.lang.IllegalArgumentException: Non-body HTTP method cannot contain #Body.
GET method does not contain body like the POST does. Here you can learn more about REST methods: https://restfulapi.net/http-methods/
EDIT: I see that you said that it works in Postman so take a look at this:
*CAN GET request have a body?
In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. ... Yes, you can send a request body with GET but it should not have any meaning.*
java.lang.IllegalArgumentException: Non-body HTTP method cannot contain #Body
This means your #GET or #DELETE should not have #Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.
#Headers("Content-Type: application/json")
#GET("helper-url")
fun getHelperUrl(
#Query("api_token") apiToken: String,
#Query("authtype") authType: String,
#Query("channel") channel: String
): Call<ResponseHelperUrl>
I want to consume athe api located at http://base-url/delivery-orders?driverId.equals=1116
This is my function:
#GET("delivery-orders")
fun getAllDeliveryOrder(
#Header("Authorization") token: String,
#Query("driverId") idDriver: Int
): Observable<OrderItems>
but I got error code 500.
Error codes from 500-599 are server errors. Meaning, your code is working but the server doesn't. Better recheck the code from the server to fix this error.
This problem usually from server..
try to check Method or maybe there has invinity looping.
My friend also get the same problem, he say you must check database. Maybe your web can't call some data in database. just try delete all or make empty database, and then run again ur Android
I found the problem about that request. the problem is the URL have driverId.equal and my network interface is
#GET("delivery-orders")
fun getAllDeliveryOrder(#Header("Authorization") token: String, #Query("driverId") idDriver: Int): Observable<OrderItems>
so retrofit think driverId on URL is driverId in #Query, so I just change "driverId" in #Query with IdDriver. that solve the problem.
In our API we have sign based on both #Query and #Path parameters. Everyone suggests to use OkHttp Interceptor for this. Everything is fine with query params, but I don't think there is any way to get path parameter values and names. For example:
/api/{version}/books/{id}
/api/v1.1/books/10
To make a correct sign, I need Map:
{"id":"10", "version":"v1.1"}
Am I missing something?
Follow the following code, probably your problem will be solved.
/api/{version}/books/{id}
public type method(#Path("version") String versionValue,#Path("id") String idValue,.....)
I wanted to use retrofit library for parsing and posting the data by passing some parameters. But When defining model class some times we will use #Serialized in-front of variable, What is the use of that Serialized.And What is the difference between #Get and #Query in passing params to API.Can Any one explain the difference.
Lets say you have api method #GET("/api/item/{id}/subitem/") so by using #Path("id") you can specify id for item in path. However your api may take additional parameters in query like sort, lastupdatetime, limit etc so you add those at end of url by #Query(value = "sort") String sortQuery
So full method will look like:
#GET("/api/item/{id}/subitem")
SubItem getSubItem(#Path("id") int itemId, #Query("sort") String sortQuery, #Query("limit") int itemsLimit);
and calling api.getSubItem(5, "name", 10) will produce url #GET("/api/item/5/subitem/?sort=name&limit=10")
and #Get is HTTP method
http://www.w3schools.com/tags/ref_httpmethods.asp says
Two commonly used methods for a request-response between a client and
server are: GET and POST.
GET - Requests data from a specified resource POST - Submits data to
be processed to a specified resource
#GET is request method. You mark method with that.
#Query is query parameter (i.e. the one in the URL). You mark method parameters with that.
#Serialized probably does not belong to Retrofit, look at its package name (move cursor there and press `Ctrl+Q in Android studio)