Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }

DataDto class:

public class DataDto {

    private String idempotency_key;
    private String amount_money;
    private String source_id;

    public DataDto(String idempotency_key, String amount_money, String source_id) {
        this.idempotency_key = idempotency_key;
        this.amount_money = amount_money;
        this.source_id = source_id;
    }
}

And lastly making the retrofit call:

 DataDto dataDto = new DataDto("ab2a118d-53e2-47c6-88e2-8c48cb09bf9b", "{\"amount\": 100, \"currency\": \"USD\"}", "cnon:CBASEITjGLBON1y5od2lsdxSPxQ");

        RetrofitInterfaces.IMakePayment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IMakePayment.class);
        Call<Void> call = service.listRepos(dataDto);
        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);
            }
        });

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;
    }
}

Edit 1: Changing to second parameter to JSON Object

JSONObject jsonObject = new JSONObject();
        try{
            jsonObject.put("amount", 100);
            jsonObject.put("currency", "USD");
        }catch (Exception e){
            Log.d(TAG, "onCreate: " + e);
        }
        DataDto dataDto = new DataDto("ab2a118d-53e2-47c6-88e2-8c48cb09bf9b", jsonObject, "cnon:CBASEITjGLBON1y5od2lsdxSPxQ");
like image 793
DIRTY DAVE Avatar asked Jun 22 '20 10:06

DIRTY DAVE


1 Answers

First of all, let's see what 400 means

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Now we are sure, the problem stands in our request (not server fault), most probably it is because you are trying to convert JSON in request (do not do this explicitly GSON will convert automatically)

Use interceptor to verify your outgoing network requests (Tell the result here)

you use @POST(".") which does not make sense, please understand BASE_URL is your server URL NOT MORE

The problem could be translating this post request

So a possible solution

  • Change base URL into "https://connect.squareupsandbox.com/"
  • Replace @POST(".") with @POST("v2/payments/")

PS. @NaveenNiraula mentioned right thing even though it did not help you, please follow his instruction, it is the correct way parsing data using GSON (make sure you include it and configure it correctly) converter

EDIT

I make it work (I eliminated 400 error code that is what you want as long as question title is concerned) partially which means I detect why 400 error was occurred and fixed it but unfortunately, I stuck the UNAUTHORIZED issue. The problem was relating to converting json and data type

data class DataDTO(
    val idempotency_key: String,
    val source_id: String,
    val amount_money: MoneyAmount

)

data class MoneyAmount(
    val amount: Int,
    val currency: String
)

I gist all code here you can refer

like image 175
Jasurbek Avatar answered Sep 19 '22 22:09

Jasurbek