Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I call a Rest API of salesforce by post method:

    client = OkHttpClient()
     val jsonIn = FormBody.Builder()              
            .add("email",URLEncoder.encode("[email protected]", "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:

@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 the result return is an error:

[{"errorCode":"UNSUPPORTED_MEDIA_TYPE","message":"Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded"}]

I had to try to change application/json to application/x-www-form-urlencoded , but still can't resolve.

I try to call a Get method, it is ok.

Why Post method occur error [Content-Type header specified in the HTTP request is not supported]?

1 Answer

0 votes
by (32.1k points)
edited by

Even though it is not mandatory to use Retrofit, these are few eye-catchy aspects which make it reliable and handy in the similar use case of yours.

Why use Retrofit?

  • Type-safe REST Adapter that makes common networking tasks easy
  • For POST operations, retrofit helps in assembling what needed to be submitted. Eg:- Generating URL encoded form.
  • Takes care of URL manipulation, requesting, loading, caching, threading, synchronization, sync/async calls
  • Helps to generate URL using type-aware generated code tied to specific REST API
  • Parsing JSON using GSON
  • Retrofit is an API adapter wrapped over OkHttp
Do you want to build a career in Salesforce? Enroll in this Salesforce training course to start your journey!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...