Description
Hello all!
First of all, RetroFit and RetroFit 2.0 are awesome, I've used them in several Android Apps in the past couple of years with great success, well, those Apps had decent backend APIs... but now... I'm working with this horribly designed API (made with ruby on rails) and I can't change the API. This nightmarish API has (among other awful things) POST methods with parameters such as:
- user[email]
- location[address]
- location[city]
And I've created this method to consume one of the methods:
@FormUrlEncoded
@POST("userExists.json")
Call<ExistsResponse> emailExists(@Field("user[email]") String email);
My service is created with a GsonConverterFactory with the following GSON object:
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.setDateFormat(WebService.API_DATE_FORMAT)
.disableHtmlEscaping()
.create();
I read that using disableHtmlEscaping() on the gson object would help but it didn't.
The problem I'm having is that the square brackets in the parameter name is getting encoded like this:
... D/OkHttp: user%5Bemail%5D=email%40example.com
(Yes!, I'm using the very neat HttpLoggingInterceptor to log the body of the requests!)
This is driving me crazy, I've tried all the possible ways to make this request and the backend API keeps sending me 404 because it is not understanding the request parameter names.
Is there a way to tell retrofit not to encode the parameter names in the body of a post request?
Your help will be greatly appreciated!
Happy holidays!
Activity
[-]Hw can I avoid request parameter to get encoded when making a form-urlencoded POST request?[/-][+]Hw can I avoid request parameter names to get encoded when making a form-urlencoded POST request?[/+][-]Hw can I avoid request parameter names to get encoded when making a form-urlencoded POST request?[/-][+]Hw can I avoid request parameter names getting encoded when making a form-urlencoded POST request?[/+]artem-zinnatullin commentedon Dec 24, 2015
AFAIK: Passed string value is not going through converter (Gson) since it's
a Form parameter. Try @field(encoded = true)
On Thu, Dec 24, 2015, 05:03 Oscar S. notifications@github.com wrote:
@artem_zin
[-]Hw can I avoid request parameter names getting encoded when making a form-urlencoded POST request?[/-][+]How can I avoid request parameter names getting encoded when making a form-urlencoded POST request?[/+]RacZo commentedon Dec 24, 2015
Digging around, I found this commit 4c38147 made by @JakeWharton that adds methods to tell if the field name or value should be encoded or not in the body.
But, I can't find a way to use them... will keep trying.
UPDATE: Ok... apparently this worked at some point in retrofit one, not retrofit2. :(
JakeWharton commentedon Dec 24, 2015
There is a test case that proves
encoded=true
works:retrofit/retrofit/src/test/java/retrofit2/RequestBuilderTest.java
Lines 1488 to 1498 in 90729eb
RacZo commentedon Dec 24, 2015
Hey thank you @JakeWharton, you rock man!,
Evidently I missed that test case. I tried with:
and the request didn't went through, then I tried with:
and that was all I had to do in order to get a response from this API.
On the other hand, there seems to be an issue with the HttpLoggingInterceptor because the output in my log is still:
hatcher521 commentedon Jan 8, 2016
i have meet this question,too.forexample:Date type,brackets
JakeWharton commentedon Jun 3, 2016
Stop spamming our issues. This is your one and only warning.
(Their comment was since deleted)
On Fri, Jun 3, 2016 at 12:41 AM AMIT SHEKHAR notifications@github.com
wrote:
sriramji commentedon Nov 10, 2016
I am facing the same issue after upgrading to Retrofit 2.0
Tried Both :
@field(value = "email", encoded = false) String email
@field(value = "email", encoded = true) String email
Getting same result "sriramji.k**%40**gmail.com", but i want "sriramji.k@gmail.com"
sriramji commentedon Nov 22, 2016
@JakeWharton Please help me out of this issue
sriramji commentedon Nov 29, 2016
This issue can be fix if you add one more method in
Formbody.Builder
likemzander commentedon Dec 19, 2016
I ran into the same issue since I upgraded from retrofit 1.9 to retofit 2.
@Field(value = "videos[]") List<Integer> videoIds
Result: videos%5B%5D=4340934
@Field(value = "videos[]", encoded = true) List<Integer> videoIds
Result: videos%5B%5D=4340934
I am not sure how the encoded parameter works but I guess this is only for the values of the field?
@sriramji did you find any solution for this?
sriramji commentedon Dec 19, 2016
@mzander For now i created my own
FormBody.Builder
like i said before11 remaining items
sriramji commentedon Sep 15, 2017
yeah that was my added method. It is not recommended but I have no other choice, so i created my own custom FormBody class
This is the class that we need to look
In Builder Inner class add your custom method
hubangmao commentedon Dec 21, 2017
The problem was finally solved !

add @headers("Content-Type:application/x-www-form-urlencoded; charset=utf-8")
KomoriWu commentedon Feb 2, 2018
还是不行
jial-apa commentedon Feb 2, 2018
Neither the extra content-type header nor setting the encoded to false works. The fields still got encoded.
michaelwiles commentedon Feb 13, 2018
The Field(encoded = true) directive is ONLY there for the scenario that the value you're sending is already encoded. Setting encoded = true does NOT disable encoding it simply ensures the value is not double encoded.
Unfortunately though, as has been mentioned, a lot of people want to turn the encoding off altogether. focussing on the behaviour of the encoded option in the Field is not the place to get this as this is not by design, for turning the encoding off - it is only to stop double encoding.
A feature request should rather be logged to add a flag to turn off encoding altogether via a @field annotation.
pmashelkar commentedon May 8, 2018
I am passing a complex POJO
@post("project/{pro_id}/sender")
Single uploadRenderingJSON(
@path("pro_id") String proId, @Body RenderRequest renderRequest);
Some fields of the POJO are already encoded using StringEscapeUtils.escapeJava(textCaption);
How can I avoid the strings getting encoded again. Since double quotes and backslash are converted to \" and \\ respectively.
Please suggest.
yusufonderd commentedon Jul 15, 2018
I'm facing same situation.
BoukhariAyoub commentedon Jul 17, 2018
I'm having this problem and nothing of the solutions above is working
Sainathhiwale commentedon Jul 27, 2018
[
{
"ofsNo": "180007",
"dispatchDate": "07/04/2018",
"vehicleNo": "ka45p3654",
"transporterName": "trns",
"depotName": "KSBCL",
"ofsId": 1
},
{
"ofsNo": "180004",
"dispatchDate": "07/04/2018",
"vehicleNo": "KA09B6435",
"transporterName": "trns",
"depotName": "KSBCL",
"ofsId": 10006
}
]
Hi every one
i want get ofsno in spinner and base on ofsno populate value into EditText like as
"dispatchDate": "07/04/2018",
"vehicleNo": "KA09B6435",
"transporterName": "trns",
"depotName": "KSBCL", etc
but using retrofit
public void networkCall(){
ofsworld = new ArrayList();
// Create an array to populate the spinner
ofsIdArrayList = new ArrayList();
final ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<List> call =apiInterface.getOfsID();
call.enqueue(new Callback<List>() {
@OverRide
public void onResponse(Call<List> call, Response<List> response) {
if (response.isSuccessful()){
Log.d("data",String.valueOf(response));
}
394090466 commentedon Feb 26, 2019
There is still no solution
sixangle commentedon Jun 4, 2019
I have tried most of the solutions above, but no one works. It took me almost half of the day to finally resolve this problem.
Api interface : (Note: do not use the @FormUrlEncoded annotation.)
Build your post parameters like as below.
That's all. Hope this could save your time.
FrancisRR commentedon Oct 30, 2019
Please Use encoded = true
@query(value = "email", encoded = true)
@http(method = "DELETE", path = "/endurl", hasBody = true)
Observable deleteContact(@query(value = "email", encoded = true) String email);
drod3763 commentedon Dec 14, 2020
I am running into this now, and none of the solutions are working. It's encoding an email and password in a form and now it's throwing errors.
SmartAppsDevelopment commentedon Jan 17, 2023
i have tried all above solution no solution worked for my problem
i am sending encrypted params in POST request witch add special character during encoding and OKHTTP adding special codes as mention above