Raw string in request body for request using modified

I want to send user: user to post request body . I am using modify lib. please suggest me.

I have already tried this

@POST(/login)
void login(@BODY String s,Callback<LoginResponse>)

      

And called it like

login("user:user",Callback<LoginResponse>)

+3


source to share


2 answers


Use TypedString for body instead of string.



+6


source


Method # 1

Think of your "user: user" as JsonObject. So please post JsonObject instead of Raw String.

You can create a POJO class:

public class User{
    public final String user;

    User(String user) {
        this.user = user;
    }
}

      

Your method looks like this:

@POST(/login)
void login(@BODY User user,Callback<LoginResponse>)

      

And call it like:

User user = new User("john");
login(user,Callback<LoginResponse>)

      

Since Retrofit uses Gson by default, User instances will be serialized as JSON as the only request body. Default content type: app / json



Method # 2

You can submit Raw String as form data with content type: application / x-www-form-urlencoded and just need the @FormUrlEncoded annotation .

Here's your method:

@FormUrlEncoded
@POST(/login)
void login(@Field("yourkey") String user,Callback<LoginResponse>)

      

you just need to replace "your key" with your api key - "user"

Call it like:

String user = "john";
login(user,Callback<LoginResponse>)

      

Ref :
How to clear all JSON in the body of the re-install
request
retrofit.http.Field retrofit.http.FieldMap

+1


source







All Articles