【问题标题】:How to convert this android code into Curl command如何将此android代码转换为Curl命令
【发布时间】:2022-01-16 19:31:14
【问题描述】:

我有以下 Android 代码

@POST("/api/oauth/token") Call<Token> login(
    @Query("client_id") String clientId, 
    @Query("client_secret") String clientSecret, 
    @Query("redirect_uri") String redirectUrl, 
    @Body() AuthBody authBody
)
import com.google.gson.annotations.SerializedName

data class AuthBody(
        @SerializedName("username") var username: String?,
        @SerializedName("password") var password: String?,
        @SerializedName("grant_type") var grantType: String = "password"
)

对于发送请求,他们使用

import retrofit2.Call;

此代码用于从后端服务器获取访问令牌。我想把它转换成Curl命令,这样我就可以在不运行这段代码的情况下发送请求。

到目前为止我所拥有的是

curl "https://example.com/api/oauth/token?client_id={client_id}&amp;client_secret={client_secret}&amp;redirect_uri={redirect_uri}" --data '{"username":"{my-username}","password":"{my-password}","grant_type":"password"}'

但是运行时,服务器响应是

{"error":"invalid_request","error_description":"translation missing: en.doorkeeper.errors.messages.invalid_request.missing_param"}

我不知道我在这里缺少什么,请帮助我。

【问题讨论】:

    标签: android curl


    【解决方案1】:

    我找到了。

    我上面的 curl 命令是正确的,但是还不够。 Curl 默认发送 header content-type: application/x-www-form-urlencoded。但是由于我们正在发送 json 数据。我们需要将其设置为content-type: application/json

    所以工作命令是

    curl "https://example.com/api/oauth/token?client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}" --data '{"username":"{my-username}","password":"{my-password}","grant_type":"password"} -H "content-type: application/json"'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-08
      • 2012-05-10
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      相关资源
      最近更新 更多