[英]Why Post method of okhttp occur error [Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded]?
I call a Rest API of salesforce by post method: 我通过post方法调用salesforce的Rest API:
url = "https://test-dev-ed.my.salesforce.com/services/apexrest/AccountUsers/"
client = OkHttpClient()
val jsonIn = FormBody.Builder()
.add("email",URLEncoder.encode("dt1@gmail.com", "UTF-8"))
.add("password", URLEncoder.encode("1","UTF-8"))
.build()
request = Request.Builder()
.post(jsonIn)
.header("Authorization", "Bearer "+accesstoken)
.addHeader("Content-Type","application/x-www-form-urlencoded")
.url(url)
.build()
response = client.newCall(request).execute()
This is rest api: 这是休息api:
@HttpPost
global static ID createUser(String email, String password) {
AccountUser__c us=new AccountUser__c();
us.Email__c=email;
us.Password__c=password;
us.Status__c=0;
insert us;
return us.Id;
}
But result return is error: 但结果返回错误:
[{"errorCode":"UNSUPPORTED_MEDIA_TYPE","message":"Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded"}]
I had try change application/json
to application/x-www-form-urlencoded
, but still can't resolve. 我曾尝试将application/json
更改为application/x-www-form-urlencoded
,但仍无法解析。
I try call a Get
method, it is ok. 我尝试调用Get
方法,没关系。
Why Post method occur error [Content-Type header specified in HTTP request is not supported]? 为什么Post方法发生错误[不支持HTTP请求中指定的Content-Type标头]?
I would like to suggest a better resolution. 我想建议一个更好的解决方案。 Retrofit Library 改造图书馆
Even though it is not mandatory to use Retrofit, these are few eye catchy aspects which makes it reliable and handy in similar use case of yours. 即使使用Retrofit并非强制要求,这些都是很少引人注目的方面,这使得它在您的类似用例中变得可靠和方便。
POST
operations, retrofit helps in assembling what needed to be submitted. 对于POST
操作,改造有助于组装需要提交的内容。 Eg:- Generating URL encoded form. 例如: - 生成URL编码形式。 JSON
using GSON
使用GSON
解析JSON
OkHttp
Retrofit是一个封装在OkHttp
的API适配器 The problem that you are facing can be resolved using retrofit like this. 您可以使用这样的改造解决您遇到的问题。
public interface APIConfiguration{
@Headers({"Accept:application/json",
"Content-Type:application/x-www-form-urlencoded"})
@FormUrlEncoded
@POST("user/registration")
Observable<DataPojo> registrationAPI(@FieldMap(encoded = true) Map<String, String> params);
}
That's it, with few annotation the library takes care of
Form URL Encoding
and related dependencies. 就是这样,只需很少的注释,库就可以处理Form URL Encoding
和相关的依赖项。
As it is inappropriate to start from corresponding Retrofit
dependencies and sample code, you can go through Reference One and Reference Two for more details. 由于从相应的Retrofit
依赖项和示例代码开始是不合适的,因此您可以查看参考一和参考二以获取更多详细信息。
As per my understanding just checkout the difference the content type header "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. 根据我的理解,只需检查差异,内容类型标题“application / x-www-form-urlencoded”对于发送大量二进制数据或包含非ASCII字符的文本效率低下。 The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data. 内容类型“multipart / form-data”应该用于提交包含文件,非ASCII数据和二进制数据的表单。 The content "multipart/form-data" follows the rules of all multipart MIME data streams. 内容“multipart / form-data”遵循所有多部分MIME数据流的规则。
https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
Also try your http request by setting your content type header as multipart/formdata. 还可以通过将内容类型标题设置为multipart / formdata来尝试您的http请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.