【问题标题】:How to add Post request Body in Url in x-www-form-urlencoded request in Kotlin?如何在 Kotlin 的 x-www-form-urlencoded 请求中在 Url 中添加 Post 请求正文?
【发布时间】:2021-11-17 05:46:35
【问题描述】:

您好,我正在尝试使用 Kotlin 进行自动化 API 测试,现在我需要添加一个发布请求以生成令牌。 post 方法包含内容类型为 application/x-www-form-urlencoded 现在我需要以 x-www-form-urlencoded 格式传递正文。我们怎样才能做到这一点。

""类登录:PostRequest() {

override fun buildHttpRequestBuilder(): HttpRequest.Builder {
    val httpRequestBuilder = super.buildHttpRequestBuilder()
    httpRequestBuilder.header("Content-Type", "application/x-www-form-urlencoded")
    return httpRequestBuilder
}

override fun buildUrl(): String {
    return "URL"
}

override fun buildPostBody(): String {

    val body = JSONObject().apply {
        .field("grant_type", "client_credentials")"""

【问题讨论】:

    标签: api kotlin generics testing web-api-testing


    【解决方案1】:

    Retrofit 1.9你可以在你的界面上添加这个注解。

    @Headers("Content-Type:application/x-www-form-urlencoded; charset=utf-8")
    

    Retrofit 2.3+ 版本在您的请求 api 中添加 @FormUrlEncoded

    演示代码

    
    @FormUrlEncoded
    @POST("mobileapi")
    Observable<Result<YourData>> queryData(@Field("pageNo") Integer num);
    
    

    希望能给你带来一些帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2021-01-19
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多