使用Retrofit调用接口时出现Error Code 415错误求助
Hey there! Let's figure out why you're hitting that frustrating 415 error when calling your forgot password API. That error means the server doesn't recognize the media type of the data you're sending—so let's break down the fixes based on your code.
First, Understand the 415 Error
415 Unsupported Media Type pops up when your client sends a request with a Content-Type the server isn't configured to accept. Looking at your code, you're using @FormUrlEncoded, which sets the request's content type to application/x-www-form-urlencoded—but maybe your backend is expecting JSON instead.
Solution 1: Switch to JSON if the Server Expects It
If your backend API accepts JSON payloads instead of form-encoded data, adjust your Retrofit setup like this:
Step 1: Update the API Interface
Replace @FormUrlEncoded and @Field with @Body, and use a data class to wrap your request parameters:
// Create a data class to hold the request payload data class ForgotPasswordRequest(val email: String) // Update your API method definition @POST("/v1/User/forgot-password") fun forgotPass(@Body request: ForgotPasswordRequest): Call<ForgotPassword>
Step 2: Adjust the Call Code
Pass the request object instead of a raw string when making the call:
val call = RetrofitHelper.instance!!.api.forgotPass(ForgotPasswordRequest(email)) call.enqueue(object : Callback<ForgotPassword> { override fun onResponse(call: Call<ForgotPassword>, response: Response<ForgotPassword>) { if (response.isSuccessful) { Log.e("response", response.body().toString()) // Handle your success logic here } else { // Log detailed error info for debugging Log.e("error", "Code: ${response.code()}, Message: ${response.message()}") } } override fun onFailure(call: Call<ForgotPassword>, t: Throwable) { // Handle network failures Log.e("failure", t.message ?: "Unknown network error") } })
Solution 2: Fix Form-Encoded Request Configuration
If your server does expect application/x-www-form-urlencoded, double-check these details:
- Confirm Retrofit Converter Setup
Make sure your Retrofit instance includes converters for handling responses (form-encoded requests don't need extra converters, but JSON responses do):
val retrofit = Retrofit.Builder() .baseUrl(YOUR_BASE_URL) .addConverterFactory(GsonConverterFactory.create()) // For parsing JSON responses .build()
- Manually Set the Content-Type Header
Sometimes the@FormUrlEncodedannotation doesn't apply the header correctly. Add it explicitly to your API method:
@POST("/v1/User/forgot-password") @FormUrlEncoded @Headers("Content-Type: application/x-www-form-urlencoded") fun forgotPass(@Field("email") email: String): Call<ForgotPassword>
Pro Tip: Test with Postman First
Before tweaking your code, test the API directly in Postman:
- Send a request to
/v1/User/forgot-passwordwith both form-encoded data and a JSON body - See which one returns a successful 200 response
- Match that format exactly in your Retrofit code
This will confirm whether the issue is a client-side setup mistake or a mismatch between what you're sending and what the server expects.
内容的提问来源于stack exchange,提问作者Ahmed S. Durrani

