Skip to content

Retrofit call returning 400, cURL request working perfectly fine, syntax issue #18

@evanli2

Description

@evanli2

I've tried making a retrofit call to an API endpoint, but it's returning a 400 error, however my curl request is working perfectly fine. I can't seem to spot the error, could someone double check my work to see where I made a mistake?

The curl call that works:

curl --request POST https://connect.squareupsandbox.com/v2/payments \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer accesstoken112233" \
    --header "Accept: application/json" \
    --data '{
    "idempotency_key": "ab2a118d-53e2-47c6-88e2-8c48cb09bf9b",
    "amount_money": {
    "amount": 100,
    "currency": "USD"},
    "source_id": "cnon:CBASEITjGLBON1y5od2lsdxSPxQ"}'

My Retrofit call:

public interface IMakePayment {

        @Headers({
                "Accept: application/json",
                "Content-Type: application/json",
                "Authorization: Bearer accesstoken112233"
        })
        @POST(".")
        Call<Void> listRepos(@Body DataDto dataDto);
    }

Retrofit Instance:

public class RetrofitClientInstance {

    private static Retrofit retrofit;
    private static final String BASE_URL = "https://connect.squareupsandbox.com/v2/payments/";


    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
public class Amount_money
{
    private String amount;

    private String currency;

    public String getAmount ()
    {
        return amount;
    }

    public void setAmount (String amount)
    {
        this.amount = amount;
    }

    public String getCurrency ()
    {
        return currency;
    }

    public void setCurrency (String currency)
    {
        this.currency = currency;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [amount = "+amount+", currency = "+currency+"]";
    }
}
public class DataDto
{
    private String idempotency_key;

    private Amount_money amount_money;

    private String source_id;

    public String getIdempotency_key ()
    {
        return idempotency_key;
    }

    public void setIdempotency_key (String idempotency_key)
    {
        this.idempotency_key = idempotency_key;
    }

    public Amount_money getAmount_money ()
    {
        return amount_money;
    }

    public void setAmount_money (Amount_money amount_money)
    {
        this.amount_money = amount_money;
    }

    public String getSource_id ()
    {
        return source_id;
    }

    public void setSource_id (String source_id)
    {
        this.source_id = source_id;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [idempotency_key = "+idempotency_key+", amount_money = "+amount_money+", source_id = "+source_id+"]";
    }
}

Making the retrofit call:

Amount_money am = new Amount_money();
        am.setAmount("100");
        am.setCurrency("USD");

        DataDto dto = new DataDto();
        dto.setIdempotency_key("55cab2a7-4d78-411b-b56d-e050864febbd");
        dto.setSource_id("cnon:CBASEPELexeldhFUrzi9u68o1ng");
        dto.setAmount_money(am);


        RetrofitInterfaces.IMakePayment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IMakePayment.class);
        Call<Void> call = service.listRepos(dto);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
                Log.d(TAG, "onResponse: " + response.toString());
            }

            @Override
            public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
                Log.d(TAG, "onFailure: Error: " + t);
            }
        });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions