Retrofit: Unprocessable entity - Error 422

Created on 7 Jan 2015  路  7Comments  路  Source: square/retrofit

Hi,

I am using Retrofit to make a POST call with access token as a BODY parameter. I am getting Response code 422 from the api. How do I make sure that the parameter (access token) that I am sending in the BODY of the POST call has the exact name that I want it to be? I want it to be access_token because that is what the api is expecting.

My service looks like this:

public interface userTokenService{
    @POST("/api/users")
    void createUser(@Body String access_token, Callback<User> cb );
}

(I was using accessToken as the parameter name but changed it to access_token expecting that it will make a difference but I don't think it does since I am still getting the same error. )

Is there are way to log BODY parameters to check the name and value or any other way by which I can solve this Unprocessable entity error.

Thanks for the help anyway.
Faizan

Most helpful comment

Wow. Awesome. That helped. Just for others who might look into this I did something like this,

private class AccessToken {
    @SerializedName("access_token")
    private String accessToken;
}

And then passed AccessToken as the BODY parameter instead of a String.

Thanks!!

All 7 comments

The parameter name has no effect. If you want arbitrary contents in the body use TypedString and put whatever you want in there. Otherwise the object is going to be run through the Converter which is by default Gson.

Hi Jake,

I really appreciate your prompt response. I am a little unsure of how to use TypedString since I have never used it before. According to what I understood from your response, this is what I have done now:

public interface MyUnfoldUserTokenService{
    @POST("/api/users")
    void createUser(@Body TypedString access_token, Callback<User> cb );
}

I don't think I have done it the right way because the error is still there. If you could guide me towards an example for TypedString being used with the POST call for custom BODY parameter names that would be great since I was unable to find one.

Thanks!

Let's start with something more simple: What content type is your server expecting?

Sure.
It's expecting: application/json; charset=utf-8

Ok, sweet. So Retrofit serializes things using Gson by default to send and receive JSON.

You need to create an object which serializes in the format you desire. Gson's object examples gives you an overview of how it works: https://sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples

Basically: create a Java object which mimics the JSON object structure that you are trying to create. The name of the class and the name of the parameter in the Retrofit service interface are arbitrary. Only the field names and their values are used.

Wow. Awesome. That helped. Just for others who might look into this I did something like this,

private class AccessToken {
    @SerializedName("access_token")
    private String accessToken;
}

And then passed AccessToken as the BODY parameter instead of a String.

Thanks!!

Trying to upload profile picture through api call using Retrofit 2 and rxjava. Response is always error which is 422 Unprocessable Entity. Giving my code and need suggestion to overcome this.

@Multipart
@POST("api/update-profile-picture")
Observable<ProfilePicture> updateProfilePicture(
    @Header("Authorization") String accessToken,
    @Part("profile_picture") RequestBody profile_picture
);


// calling presenter method to update profile picture
public void updateProfilePictureImage(File file){
getMvpView().showProgress();

RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);

oyeBuddyService.updateProfilePicture("Bearer" + " " + mPrefs.getOyeUserAccessToken(), reqFile)
        .subscribeOn(mNewThread)
        .observeOn(mMainThread)
        .subscribe(new Observer<ProfilePicture>() {
            @Override
            public void onCompleted() {
                getMvpView().hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().hideProgress();
                Log.e("error: ",e.getMessage());
            }

            @Override
            public void onNext(ProfilePicture profilePicture) {
                Log.e("response: ", profilePicture.toString());
                getMvpView().onProfilePictureUpdated(profilePicture.profile_picture_url);
            }
        });

}

Response --->

RetrofitModule: Received response for http://174.138.64.95/api/update-profile-picture in 4540.6ms
Date: Wed, 04 Oct 2017 10:30:53 GMT
Server: Apache/2.4.18 (Ubuntu)
Vary: Authorization
Cache-Control: no-cache, private
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Content-Length: 99
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: application/json

E/error:: HTTP 422 Unprocessable Entity

Was this page helpful?
0 / 5 - 0 ratings