4

So I need to send an API request in this format

{ "access_key": "6477848488cchfc47488", "person": { "first_name": "John", "last_name": "Henry", "email": "john@henry.com" } }

I have created an object

public class Person {
    public String first_name = "";
    public String last_name = "";
    public String email = "";
}

In my interface I have

@FormUrlEncoded
@POST("/send_details")
void sendDetails(@Field("person") Person person, @Field("access_key") String accessKey, Callback<User> cb);

Finally in my Activity I have the code below to call the send details method

Person person = new Person("John", ":"Henry, "john@henry.com");
  aApi.sendDetails(person, ACCESS_KEY, new Callback<User>() {
     @Override
        public void success(User user, Response response) {

        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
  }

I get a 500 Internal Server Error. I've just switched from volley to retrofit. Would appreciate any help.

Amanni
  • 1,924
  • 6
  • 31
  • 51

1 Answers1

0

Try using @Body annotation instead of @Field and passing a single Body object.

class DetailsBody {
    @SerializedName("access_key")
    public String accessKey;
    public Person person;

    public DetailsBody(String accessKey, Person person) {
        this.accessKey = accessKey;
        this.person = person;
    }
}

and then:

@POST("/send_details")
void sendDetails(@Body DetailsBody body, Callback<User> cb);

(without @FormUrlEncoded)

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132