How can I use Retrofit for POST for a complex JSON parameter

The mail request I need to do should look like this

{
"project": {
    "name": "newname123",
    "identifier": "id55"},
"key":"8f583ad25100575b974062e0cee43e47aa158e4e"}

I was able to send it to the server using a raw implementation in Postman, but I don’t know how to send the same using form data

Here is my interface

@FormUrlEncoded
@POST("projects.json")
Call<Project> CreateProject(@Field(value = "project") ProjectToSend project,
                            @Field("key") String key);

Regardless of whether I try to do this with @body or @field, this does not work.

Class ProjectToSend

public class ProjectToSend {
private String name;
private String identifier;

public ProjectToSend(String name, String identifier) {
    this.name = name;
    this.identifier = identifier;
+4
source share
2 answers

create two classes to represent full JSON, something like this:

public class Project {
    private ProjectToSend project;
    private String key;

    // getters and setters
}

public class ProjectToSend {
    private String name;
    private String identifier;

    // getters and setters
}

and then call:

@POST("put/the/url/here")
Call<Project> createUser(@Body Project project);
0
source

You can try this site to create complex POJO classes: JSON schema 2 pojo

0

All Articles