简体   繁体   English

Retrofit2:在 Retrofit POST 请求中添加令牌参数(@Query)

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

I'm using retrofit2 for a mobile app.我正在为移动应用程序使用改造 2。 The API I'm interacting with requires the Token to be put in the params inorder to get make a valid request.我正在与之交互的 API 需要将令牌放入参数中才能发出有效请求。 Like this:像这样: 在此处输入图片说明

I want to make a POST request wherein I can integrate that token into the params.我想发出一个 POST 请求,在其中我可以将该令牌集成到参数中。

在此处输入图片说明

I tried using @Query with no luck, and the Retrofit documentation only demonstrates it in the @GET request.我尝试使用 @Query 但没有成功,Retrofit 文档仅在 @GET 请求中演示了它。 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. @Query 直接在 POST 请求中将我传递的令牌直接放入参数中就好了。

When you need to add path value dynamically use @Path当您需要动态添加路径值时,请使用@Path

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

And remove @Query并删除@Query

  • form-urlencoded: POST表单 urlencoded: POST
  • query parameter: GET查询参数:GET

Use form-urlencoded requests to send data to a server or API.使用表单 urlencoded 请求将数据发送到服务器或 API。 The data is sent within the request body and not as an url parameter.数据在请求正文中发送,而不是作为 url 参数发送。

Query parameters are used when requesting data from an API or server using specific fields or filter.当使用特定字段或过滤器从 API 或服务器请求数据时使用查询参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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