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?
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.
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.
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.
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,...);
In retrofit 2 is a little bit different:
@FormUrlEncoded @POST/GET/PUT/DELETE("/your_endpoint") Call<Task> createTask (@Field("your_field") String title);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With