Prevent retrofit from encoding my http request body

后端 未结 4 1557
刺人心
刺人心 2021-01-11 11:30

I\'m trying to pass a string of the format below as the body of a http post request.

param1=PARAM1¶m2=PARAM2¶m3=PARAM3

But

4条回答
  •  春和景丽
    2021-01-11 12:30

    To answer the question directly, you can use TypedString as the method parameter type. The reason the value is being changed is because Retrofit is handing the String to Gson in order to encode as JSON. Using TypedString or any TypedOutput subclass will prevent this behavior, basically telling Retrofit you will handle creating the direct request body yourself.

    However, that format of payload is called form URL encoding. Retrofit has native support for it. Your method declaration should actually look like this:

    @FormUrlEncoded
    @POST("/oauth/token")
    void getAccessToken(
        @Field("param1") String param1,
        @Field("param2") String param2,
        @Field("param3") String param3,
        Callback callback);
    

提交回复
热议问题