Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Retrofit: content type as application/x-www-form-urlencoded

Fairly new to android development. I am trying to use retrofit to send a post request. In my retrofit logs, I am seeing

Content-Type: text/plain; charset=utf-8 

I found that requests will only work if I use the content type:

application/x-www-form-urlencoded 

I have searched the googles and have found no clear way to explicitly set the content type. Anyone know how to do it?

like image 762
kinezu Avatar asked Feb 03 '15 03:02

kinezu


People also ask

What is form Urlencoded in retrofit?

Annotation Type FormUrlEncodedDenotes that the request body will use form URL encoding. Fields should be declared as parameters and annotated with @Field . Requests made with this annotation will have application/x-www-form-urlencoded MIME type.

How do you send POST request with X www form Urlencoded body in Java HttpURLConnection?

String urlParameters = cafedra_name+ data_to_send; URL url; HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection)url. openConnection(); connection. setRequestMethod("POST"); connection.

What is form URL encoded?

The application/x-www-form-urlencoded content type describes form data that is sent in a single block in the HTTP message body. Unlike the query part of the URL in a GET request, the length of the data is unrestricted.


2 Answers

In the class where you define your service, modify the related method to follow the pattern below:

@FormUrlEncoded @POST/GET/PUT/DELETE("/your_endpoint") Object yourMethodName(@Field("your_field") String yourField,...); 
like image 127
k3v1n4ud3 Avatar answered Sep 29 '22 15:09

k3v1n4ud3


In retrofit 2 is a little bit different:

@FormUrlEncoded @POST/GET/PUT/DELETE("/your_endpoint") Call<Task> createTask (@Field("your_field") String title);  
like image 22
Giannis Papadopoulos Avatar answered Sep 29 '22 17:09

Giannis Papadopoulos