简体   繁体   English

为什么发生okhttp的Post方法错误[不支持HTTP请求中指定的Content-Type头:application / x-www-form-urlencoded]?

[英]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并非强制要求,这些都是很少引人注目的方面,这使得它在您的类似用例中变得可靠和方便。

Why to use Retrofit? 为什么要使用Retrofit?

  • Type-safe REST Adapter that makes common networking tasks easy 类型安全的REST适配器,可以轻松完成常见的网络任务
  • For POST operations, retrofit helps in assembling what needed to be submitted. 对于POST操作,改造有助于组装需要提交的内容。 Eg:- Generating URL encoded form. 例如: - 生成URL编码形式。
  • Takes care of URL manipulation, requesting, loading, caching, threading, synchronization, sync/async calls 负责URL操作,请求,加载,缓存,线程,同步,同步/异步调用
  • Helps to generate URL using type-aware generated code tied to specific REST API 帮助使用与特定REST API绑定的类型感知生成代码生成URL
  • Parsing JSON using GSON 使用GSON解析JSON
  • Retrofit is an API adapter wrapped over 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.

相关问题 如何使用“ Content-type:application / x-www-form-urlencoded”发出Okhttp请求? - How to make an Okhttp Request with “Content-type:application/x-www-form-urlencoded”? '如何使用带有“Content-Type”的 Volley 发送 Post 请求:“application/x-www-form-urlencoded” - 'How to send Post request using Volley with “Content-Type”:“application/x-www-form-urlencoded” 内容类型为 application/x-www-form-urlencoded 的 HTTP Post 请求在 Spring 引导服务中不起作用 - HTTP Post request with content type application/x-www-form-urlencoded not working in Spring boot service 如何使用内容类型 x-www-form-urlencoded android 改造发送帖子请求 - How to send post request with content-type x-www-form-urlencoded android retrofit 更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML - Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded 无法在Android HTTP POST调用中将CONTENT_TYPE更改为application / x-www-form-urlencoded - Cannot change CONTENT_TYPE to application/x-www-form-urlencoded in Android HTTP POST call 如何使用内容类型为x-www-form-urlencoded的okhttp库发布数据? - How do I post data using okhttp library with content type x-www-form-urlencoded? Android Retrofit:内容类型为application / x-www-form-urlencoded - Android Retrofit: content type as application/x-www-form-urlencoded 如何获取登录 api 的响应以使用 volley 对内容类型为 application/x-www-form-urlencoded 的此 json 数据执行登录 - How to get response for login api to perform login using volley for this json data with Content-type: application/x-www-form-urlencoded 为什么Volley String请求不能在x-www-form-urlencoded中使用POST方法发送Body参数? - why can't Volley String request send Body parameters with POST method in x-www-form-urlencoded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM