Getting error code 400 bad request in retrofit but works on postman
The issue is with your locationPost
type, it should be JsonObject
and not JSONObject
, so try one of the following approaches
Approach 1
Api Interface
Call<ResponseBody> registerUser(@Body JsonObject locationPost);
Api call
JsonObject obj = new JsonObject();
obj.addProperty("httpMethod","POST");
obj.addProperty("firstname",firstNameValue);
// add the rest of the field
Call<ResponseBody> call = apiInterface.registerUser(obj);
//rest of the logic remains same
Approach 2
create a POJO class representing the object and pass the instance of the object
public class RequestObject{
final String httpMethod, firstname;
// declare member variables for all the keys
RequestObject(String method,String firstname){
this.httpMethod = method;
this.firstname = firstname;
}
}
API Interface
Call<ResponseBody> registerUser(@Body RequestObject locationPost);
Api call
RequestObject requestobject = new RequestObject("POST","firstName");
// add the rest of the field
Call<ResponseBody> call = apiInterface.registerUser(requestObject);
//rest of the logic remains same
Retrofit gets 400 Bad request but works with postman
1.Frist thing your api is a GET Method so use @GET instead @POST
Second try to change url base url in retrofit
.baseUrl("https://locodealapi.herokuapp.com")
to .baseUrl("https://locodealapi.herokuapp.com/")
this will work. or leave your problem in comment
2.this is sample code
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException{
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder =original.newBuilder().
method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.addInterceptor(interceptor).connectTimeout(60,TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://locodealapi.herokuapp.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient).build();
UserApi userApi = retrofit.create(UserApi.class);
Call<ResponseBody> call = userApi.deals("your token");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,retrofit2.Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {}
});
}
@GET("api/deals")
Call deals(@Header("x-access-token") String x_access_token);
Android Retrofit 2: Random code 400 (bad request) response
So I fixed this my reinitializing OkHttpClient every ew request. Probably it has some old data in it. If anyone can elaborate on this, would be great.
Need to get the response message, while getting 400 error in Retrofit, Android
Try to use this to map your response to the format of the error supplied by the API:
public static ErrorDTO parseError(Retrofit retrofit, Response<?> response) {
Converter<ResponseBody, ErrorDTO> converter = retrofit.responseBodyConverter(ErrorDTO.class, new Annotation[0]);
ErrorDTO error;
try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
return new ErrorDTO();
}
return error;
}
ErrorDTO should be a simple POJO which matches the structure of the body of the error response.
Retrofit POST method returns error 400
UPDATE: change field name in Tokken class from @SerializedName("tokken")
into @SerializedName("token")
as on attached screenshot from Postman in response you get "token"
key but not "tokken"
.
Maybe it's not the root cause but one of the problem for sure:
you didn't configure your retrofit client properly, you missed to add http client:
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
Related Topics
Remove Green Border Around Android Application
Android Set Height and Width of Custom View Programmatically
How to Transfer from One Fragment to Another in Android
How to Remove the Unnecessary Top Padding of the Navigation View
How to Disable Spinner in Android and Enable It on Button Click
Android Scrollview Not Scrolling Down After Keyboard Opens
How to Change Styling of Textinput Placeholder in React Native
How to Use Sharedpreferences in Android to Store, Fetch and Edit Values
How to Set Default Font Family For Entire Android App
How to Set Value for Each Item Button Increase and Decrease Click and Set That Value in Text View
Android Blur View (Blur Background Behind the View)
Photo Rotated from Camera (Samsung Device)
Onclick() to Change Button Border Color Rather Changing Button Color
How to Resize the Avd Emulator Window in Android Studio
Alarm Manager Won't Work When the App Is Killed in the Background and Device Is Locked
Viewmodel Onchange Gets Called Multiple Times When Back from Fragment