wuntee
wuntee

Reputation: 12470

Java Apache HTTPClient do not encode POST entity

I have an endpoint that requires an 'authenticity_token' that is in the format like:

Iq2rNXN+OxERv+s6TSloJfKkPZVvqnWe1m0NfODB5OI=

However, sometimes it has "special" characters, such as:

E7IzeP73OgPGgXM/up295ky1mMQMio2Nb8HMLxJFyfw=

This gets encoded to:

E7IzeP73OgPGgXM%26%2347%3Bup295ky1mMQMio2Nb8HMLxJFyfw%3D

For some reason, the endpoint does not like the encoding of those special characters and will think the token is invalid. Is it possible to add a POST variable that does not encode specific values? I am currently doing something like:

    HttpPost post = new HttpPost(URL + NEW_FINDING);
    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("foo", foo));
    nvps.add(new BasicNameValuePair("authenticity_token", authenticityToken));
    post.setEntity(new UrlEncodedFormEntity(nvps));

Upvotes: 0

Views: 4308

Answers (1)

jdb
jdb

Reputation: 4509

You can always use ByteArrayEntity or StringEntity instead of UrlEncodedFormEntity and do the encoding yourself. It should look something like foo=var1&bar=var2.
You have to set Content-Type=application/x-www-form-urlencoded
You may want to find out what your endpoint expects as a charset parameter for the application/x-www-form-urlencoded value of the Content-Type header. Then pass it as a parameter to the UrlEncodedFormEntity constructor. This should be the right fix.

Upvotes: 2

Related Questions