3

i want to send user:user in the post request body. I am using retrofit lib. please suggest me.

I have tried already like this

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

And called this as

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

Arun M R Nair
  • 653
  • 7
  • 30
Vinod Kumar
  • 137
  • 1
  • 11

2 Answers2

6

Use a TypedString for the body instead of a String.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
1

Method #1

Consider your "user:user" is seem to a JsonObject. So please post JsonObject instead of Raw String.

You can construct a POJO class:

public class User{
    public final String user;

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

Your method like this:

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

And call this as:

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

Since Retrofit uses Gson by default, the User instances will be serialized as JSON as the sole body of the request. Default use content-type:application/json

Method #2

You just can post Raw String as Form Data with content-type:application/x-www-form-urlencoded and just need a @FormUrlEncoded annotation.

Here is your method:

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

you just need to replace "yourkey" to your api's key - "user"

Call this as:

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

Reference:
How to POST raw whole JSON in the body of a Retrofit request
retrofit.http.Field
retrofit.http.FieldMap

Community
  • 1
  • 1
JohnWatsonDev
  • 1,227
  • 9
  • 16