0

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));

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that id like to use the UrlEncodedFormEntity for the rest of the parameters, I just need to be able to have the authenticity_token not encoded...
Just use the first option then: ByteArrayEntity or StringEntity with Content-Type=application/x-www-form-urlencoded. UrlEncodedFormEntity does exactly this but it encodes the parameters for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.