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

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值