Home > Enterprise >  How to convert POST curl request to Retrofit Request in Android?
How to convert POST curl request to Retrofit Request in Android?

Time:06-05

I'm trying to represent the below curl request in android using Retrofit,

curl -i -X POST \
  https://graph.facebook.com/v13.0/1234567890/messages \
  -H 'Authorization: Bearer  ucuzZCQv9qb--token--0UMHaEhLwvuOW6WvapUGuPAkrDchj' \
  -H 'Content-Type: application/json' \
  -d '{ "messaging_product": "whatsapp", "to": "9477number", "type": "template", "template": { "name": "hello_world", "language": { "code": "en_US" } } }'

My API Service class

public interface APIService {
    @POST("{phoneNoId}/messages")
    Call<MsgResponse> SendMsg(@Path("phoneNoId") String phoneNoId,
                              @Header("Authorization") String authorization,
                              @Header("Content-Type") String types,
                              @Body String msgObj
    );
}

My API calling place in the MainActivity.class

APIService apiService=RetroInstance.getRetrofitClient().create(APIService.class);

            Call<MsgResponse> call=apiService.SendMsg("100798385993185",
                    "Bearer 5iWIZBiI5vogoNZBKbBJ0oZAibvZBG---token--xIcDPoW",
                                                     "application/json",
                                                     jsonBody
                                                    );

            Log.d(TAG, "onCreate: " call.toString());

            call.enqueue(new Callback<MsgResponse>() {
                @Override
                public void onResponse(Call<MsgResponse> call, Response<MsgResponse> response) {
                    Log.d(TAG, "onResponse: " response.toString());
                }

                @Override
                public void onFailure(Call<MsgResponse> call, Throwable t) {
                    Log.d(TAG, "onFailure: " t.getMessage());
                    Log.d(TAG, "onFailure: " t.getStackTrace().toString());
                }
            });

here jsonbody I'm getting the json object using gson like below String jsonBody=gson.toJson(msgObj);

My Request is successful but I'm getting 400 error code error message below, what went wrong here, Thank you

onResponse: Response{protocol=h2, code=400, message=, url=https://graph.facebook.com/v12.0/1234567890/messages}

enter image description here

CodePudding user response:

You can pass headers to your Retrofit request using Interceptor. This might not be a perfect way, but it works quite good, These headers are sent with every API request.

First, extend Interceptor and add the header to the request as shown below:

class LoggingInterceptor implements Interceptor {
  @Override public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();
    Request newRequest = request.newBuilder()
       .header("Authorization","YOUR AUTH CODE HERE")
       .header("Content-Type","application/json").build(); 
    return chain.proceed(newRequest);
  }
}

Now you can use this custom Interceptor with OkHttpClient :

OkHttpClientBuilder builder = OkHttpClient.Builder()
    .addInterceptor(new MyInterceptor());

Use this client while building the Retrofit object:

Retrofit.Builder()
            .baseUrl(YOUR_BASE_URL)
            .client(okHttpClientBuilder.build())
            .build()

CodePudding user response:

Everything is well explained in this video here,

  • Related