I have a post request that will send details to the server. The condition is that each user was assigned a unique value to and this value must be passed when the user want to place the request. I have a model class that is passed through the @Body
annotation but I don't know how to pass this user's unique key along with this model class.
The key was passed in the backend as a request parameter. Please note that this token is not as an `Authorization header`, it's just a unique token that was assigned to each user for authentication purpose
Here is how the backend looks like It was developed with `Spring boot`
@PostMapping("appointment/book")
public ResponseEntity<ApiResponse> bookAppointment(@RequestBody AppointmentBookingDto appointmentBookingDto, @RequestParam("token") String token) throws DataNotFoundException, ParseException, DataAlreadyExistException {
return appointmentBookingService.bookAppointment(appointmentBookingDto,token);
}
This is how I tried to pass it in my interface class in android studio
@POST("appointment/book")
Call<ApiResponse> bookAppointment(@Body AppointmentBookingDto appointmentBookingDto, @Path("token") String token);
Here is the activity that I called the bookAppointment method.
private void bookAppointment() {
progressBar.setVisibility(View.VISIBLE);
date = btnDate.getText().toString();
time = btnTime.getText().toString();
AppointmentBookingDto appointmentBookingDto = new AppointmentBookingDto(time, date, purpose.getText().toString());
Call<ApiResponse> call = BaseUrlClient
.getInstance().getApi().bookAppointment(
appointmentBookingDto, token.getText().toString()
);
call.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
if (response.code() == 201) {
progressBar.setVisibility(View.GONE);
Log.d("Main",response.body().getMessage());
Toast.makeText(BookAppointmentActivity.this, response.message(), Toast.LENGTH_SHORT).show();
} else {
try {
progressBar.setVisibility(View.GONE);
Log.d("Main", "Response code is " response.code());
Log.d("Main", "Error message is " response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Log.d("Main", "Failure message is " t.getMessage());
Toast.makeText(BookAppointmentActivity.this, "Error " t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
CodePudding user response:
You are asking for how to pass parameter
in retrofit but in your backend you are accepting query
for example -
You are asking for -
https://yourdoman.com/appointment/book/12345
But your spring backend accepting something like this -
https://yourdoman.com/appointment/book?token=12345
so you can solve this issue by either changing in backend or frontend (Android)
I'm showing you both
1. Solve by changing only frontend which is android -
You can can change @Path
to @Query
like this :
@POST("appointment/book")
Call<ApiResponse> bookAppointment(@Body AppointmentBookingDto
appointmentBookingDto, @Query("token") String token);
2. Solve by changing only backend (Spring)
Note : I didn't used spring yet but I have noticed this from doc.
Instead of using @RequestParam
use @PathVariable
something like this :
@PostMapping("appointment/book/{token}")
public ResponseEntity<ApiResponse> bookAppointment(@RequestBody AppointmentBookingDto appointmentBookingDto,
@PathVariable String token) throws DataNotFoundException, ParseException, DataAlreadyExistException {
return appointmentBookingService.bookAppointment(appointmentBookingDto,token);
}
and make sure you only apply one solution don't change from both