Alon Shlider
Alon Shlider

Reputation: 1298

How do I add a JSON body to my post request using Retrofit?

I am trying to send a POST request using Retrofit library.

Here is my MarketApiCalls interface and my networking method doing the work -

public interface MarketApiCalls {


    @POST("api/Search/Vendor/Multiple")
    Call<String> getVendors(
            @Query("take") int take,
            @Query("page") int page,
            @Body String json
    );
}
private void initNetworking() {
        String body = "[{ \"filters\": { \"VendorName\": { \"value\": [\"*\"], \"cretiria\": 0, \"type\": 5 } } }]"
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://search.myverte.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        marketApiCalls = retrofit.create(MarketApiCalls.class);

        Call<String> vendorsCall = marketApiCalls.getVendors(9, 0, body);
        vendorsCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(getContext(), "Not successful", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.d("response body", response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

Here is the JSON I need to attach to the POST request as a body -

[{
    "filters": {
        "VendorName": { 
            "value": ["*"],
            "cretiria": 0,
            "type": 5
        }
    }
}]

The issue is that I am getting error code 400. It does not specify that the body is missing or corrupt , just gives 400 error saying the following error -

enter image description here

What am I missing? I am suspicious that I am not giving the body as needed.

edit -

I have tried the following solution yet the same error occurs -

public class VendorBodyModel {
    private Filters filters;

    public VendorBodyModel() {
    }

    public VendorBodyModel(Filters filters) {
        this.filters = filters;
    }

    public Filters getFilters() {
        return filters;
    }

    public void setFilters(Filters filters) {
        this.filters = filters;
    }
    public class Filters {
        private VendorName vendorName;

        public Filters() {
        }

        public Filters(VendorName vendorName) {
            this.vendorName = vendorName;
        }
    }

    public class VendorName {
        private String[] value;
        private int cretiria;
        private int type;

        public VendorName(String[] value, int cretiria, int type) {
            this.value = value;
            this.cretiria = cretiria;
            this.type = type;
        }

        public String[] getValue() {
            return value;
        }

        public void setValue(String[] value) {
            this.value = value;
        }

        public int getCretiria() {
            return cretiria;
        }

        public void setCretiria(int cretiria) {
            this.cretiria = cretiria;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }
}
public interface MarketApiCalls {


    @POST("api/Search/Vendor/Multiple")
    Call<String> getVendors(
            @Query("take") int take,
            @Query("page") int page,
            @Body VendorBodyModel bodyModel
    );
}
private void initNetworking() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://search.myverte.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        marketApiCalls = retrofit.create(MarketApiCalls.class);
        String[] array = {"*"};
        int cretiria = 5;
        int type = 0;
        VendorBodyModel.VendorName vendorName = new VendorBodyModel().new VendorName(array, 5, 0);
        VendorBodyModel.Filters filters = new VendorBodyModel().new Filters(vendorName);
        VendorBodyModel vendorBodyModel = new VendorBodyModel(filters);

        Call<String> vendorsCall = marketApiCalls.getVendors(9, 0, vendorBodyModel);
        vendorsCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(getContext(), "Not successful", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(getContext(), "FUCKING SUCCESS!!!", Toast.LENGTH_SHORT).show();
                Log.d("response body", response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

What am I missing?

Upvotes: 0

Views: 82

Answers (2)

Antonis Radz
Antonis Radz

Reputation: 3097

Just simply create post model class like this:


class Body {
    Filters filters;
    int parameter2;
}

class Filters {
    VendorName VendorName;
    .......
    .......
    .......
}

and use it like this:

 @POST("api/Search/Vendor/Multiple")
    Call<String> getVendors(
            @Query("take") int take,
            @Query("page") int page,
            @Body Body json
    );

And also make sure that Body class field namings matches required namings, also dont forget that list or array is marked with [...], simple pojo {...}

Here is full body class in Kotlin:

data class Body(
    @SerializedName("filters")
    val filters: Filters? = Filters()
)

data class Filters(
    @SerializedName("VendorName")
    val vendorName: VendorName? = VendorName()
)

data class VendorName(
    @SerializedName("value")
    val value: List<String?>? = listOf(),
    @SerializedName("cretiria")
    val cretiria: Int? = 0,
    @SerializedName("type")
    val type: Int? = 0
)

Upvotes: 1

Ghoul
Ghoul

Reputation: 46

Instead of string create JsonObject and send it as @Body JsonObject request

Upvotes: 0

Related Questions