Search code examples
kotlindjango-rest-frameworkretrofit

Why is it not possible to transfer the user token to the API django rest framework?


There is an API based on the django rest framework, which outputs an order model in which there is a user token, order date, products, and price.

    {
        "userToken": "7e0e72e274bbda5e6f3cc9e3563f8c41384a37af",
        "date": "2023-05-14T08:11:05.689723Z",
        "product": [
        ...
        ],
        "price": 170
    },

And I am now trying to get data on the token, if you enter this url in the browser bar, you can get a list of orders made by a certain user.

base_url/Order/Order/?search=7e0e72e274bbda5e6f3cc9e3563f8c41384a37af

I am trying to get all the orders made by the user using this function

@GET("Order/Order/")
suspend fun getOrder(@Query("userToken") userToken:String):Response<List<OrderResult>>

But in order to receive orders made by a certain user, I receive absolutely all orders. After that, I tried to change the url, but it didn't work, I still received absolutely all orders.

@GET("Order/Order/?search=")
suspend fun getOrder(@Query("userToken") userToken:String):Response<List<OrderResult>>

After the Query request failed, I tried using Path, but nothing came from Path at all.

@GET("Order/Order/?search={userToken}")
suspend fun getOrder(@Path("userToken") userToken:String):Response<List<OrderResult>>

Based on the above, how to make sure that orders related only to a certain user come?

I tried to write a Path and Query in anticipation of the appearance of orders associated with a specific user


Solution

  • When you want to add a query param to the request, you need to use the query value to match the parameter name, in your case it would be search not userToken in @Query

    //replace "userToken" with "search" inside Query
    @GET("Order/Order/")
    suspend fun getOrder(@Query("search") userToken: String):Response<List<OrderResult>>