Bootstrap

post请求改成body_改造@Body在HTTP请求中显示为参数

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);

解决方案

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 callback

);

;