简体   繁体   中英

Retrofit2: Adding Token Params(@Query) in Retrofit POST Request

I'm using retrofit2 for a mobile app. The API I'm interacting with requires the Token to be put in the params inorder to get make a valid request. Like this: 在此处输入图片说明

I want to make a POST request wherein I can integrate that token into the params.

在此处输入图片说明

I tried using @Query with no luck, and the Retrofit documentation only demonstrates it in the @GET request. Any idea on how I might achieve this?

My issue was just an error in my input. This works. @Query directly in the POST Request puts my passed token directly into the params just fine.

When you need to add path value dynamically use @Path

@FormUrlEncoded
@POST("users/{token}")
Call<AgentResponse> agentCreateUse(
        @Path("token") String token);
...
        )

And remove @Query

  • form-urlencoded: POST
  • query parameter: GET

Use form-urlencoded requests to send data to a server or API. The data is sent within the request body and not as an url parameter.

Query parameters are used when requesting data from an API or server using specific fields or filter.

在 POST 请求中使用 @Field 而不是 @Query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM