Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit @Body showing up as parameter in HTTP request

I've previously used Square's Retrofit successfully for a @GET web API call but when trying to send JSON as the @BODY in a @POST call, on the server (Rails) the JSON is showing up as Parameters rather than the body request.

My understanding is that @BODY will add that method parameter to the request in the body.

Any idea what I'm doing wrong?

WebApi:

@POST("/api/v1/gear/scans.json")
Response postScans(
    @Header(HEADER_AUTH) String token,
    @Body JsonObject scans
);

Make web request:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setServer(api_url)
    .build();
WebApi webApi = restAdapter.create(AssetsWebApi.class);     
Response response = webApi.postScans(auth_token, valid_json);
like image 289
saywhatnow Avatar asked Sep 20 '13 03:09

saywhatnow


1 Answers

Turns out that if you want to POST data as part of the request body, you need to annotate the API interface method as @FormUrlEncoded and pass the content of the body as a @Field as below:

@FormUrlEncoded
@POST("/api/v1/gear/scans.json")
Response postScans(
    @Header(HEADER_AUTH) String token,
    @Field("scans") JsonArray scans
);

Async call for @Rickster:

@POST("/api/v1/gear/scans.json") 
void postScans(
    @Header(HEADER_AUTH) String token,
    @Body JsonObject scans,
    Callback<PostSuccessResponseWrapper> callback
);
like image 143
saywhatnow Avatar answered Sep 25 '22 09:09

saywhatnow