防止改造编码我的http请求正文

编程入门 行业动态 更新时间:2024-10-25 11:33:30
本文介绍了防止改造编码我的http请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试传递以下格式的字符串作为http发布请求的正文。

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

param1=PARAM1&param2=PARAM2&param3=PARAM3

但是改装对我的身体进行编码,以便=成为\ u003d和&成为\ 0000。我最终得到一个字符串实际上是这样的:

But retrofit encodes my body so that = becomes \u003d and & becomes \u0026. And I end up with a string which actually looks like this:

param1\u003dPARAM1\u0026param2\u003dPARAM2\u0026param3\u003dPARAM3

如何防止这种情况?

我的改造休息api定义如下。

My retrofit rest api is defined as follows.

public interface RestAPI { @POST("/oauth/token") public void getAccessToken(@Body String requestBody, Callback<Response> response); }

推荐答案

直接回答这个问题,您可以使用 TypedString 作为方法参数类型。更改值的原因是因为Retrofit将 String 交给Gson以便编码为JSON。使用 TypedString 或任何 TypedOutput 子类将阻止此行为,基本上告诉Retrofit您将自己处理创建直接请求正文。

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.

但是,这种有效载荷格式称为表格URL编码。 Retrofit对它有本机支持。您的方法声明实际上应如下所示:

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

更多推荐

防止改造编码我的http请求正文

本文发布于:2023-10-09 18:42:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正文   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!