Home > database >  How to convert this android code into Curl command
How to convert this android code into Curl command

Time:12-25

I have the following Android code

@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"
)

For sending request, they use

import retrofit2.Call;

This code is used to get access token from backend server. I want to convert it into Curl command, so I can send request without running this code.

What I have so far is

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"}'

But when running, the server response is

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

I don't know what I am missing here, please help me.

CodePudding user response:

I found it.

My curl command above is correct, however, it is not enough. Curl sends header content-type: application/x-www-form-urlencoded by default. But since we are sending json data. We need to set it as content-type: application/json.

So the working command is

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"'
  • Related