1

I want to perform this command with Apache http-components (4.1.2)

 curl  --data "strings[]=testOne&string-keys[]=test.one&strings[]=testTwo&string-keys[]=test.two&project=Test" https://api.foo.com/1/string/input-bulk

The target api need strings and string-keys parameters as array, which mean repeating strings[] and string-keys[] for each parameter.

This curl command works fine but with Http-component, while I got exactly the same parameters.

Maybe I'm doing something wrong.

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add( new BasicNameValuePair( "project", PROJECT_NAME ) );

    for ( Entry entry : newEntries )
    {
        params.add( new BasicNameValuePair( "string-keys[]", entry.getKey() ) );
        params.add( new BasicNameValuePair( "strings[]", entry.getValue() ) );
        params.add( new BasicNameValuePair( "context[]", "" ) );
    }

    URI uri = URIUtils.createURI( "https", "api.foo.com", -1, "/1/string/input-bulk", null, null );

    UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity( params );
    logger.info( "POST params : {}", EntityUtils.toString( paramEntity ) );
    HttpPost httpRequest = new HttpPost( uri );
    httpRequest.setEntity( paramEntity );

    HttpResponse response = new DefaultHttpClient().execute( httpRequest );

The POST params looks like :

POST params : project=Test&string-keys%5B%5D=test.one&strings%5B%5D=TestOne&string-keys%5B%5D=test.two&strings%5B%5D=TestTwo

If I put them behind --data in curl, it works, but not with HttpCoponents. Can someone explain me why?

Thanks in advance

1 Answer 1

2

Try adding the header "application/x-www-form-urlencoded" in your httpRequest


    httpRequest.addHeader("content-type", "application/x-www-form-urlencoded");
    HttpResponse response = new DefaultHttpClient().execute( httpRequest );

Hopefully that will work

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

6 Comments

Have you tried using StringEntity instead of UrlEncodedFormEntity?
Ok so using StringEntity doesn't change anything, it seems my problem has nothing to do with encoding. I update my question.
Let's try one more thing. I read in several places that input form with same name should be group together and separated by comma. Maybe curl does the grouping for you and Android doesn't. So for strings[] with value1 and value2, you should have strings[]=value1,value2 in the key value pair.
thanks but I decided to switch to httpclient 3.x, and all work like a charm (+1 for your help ;))
@tbruyelle What worked ? You are using String Entity ? I have similar issue...I want to send List<Long> ...not sure how to do that. I am making webservice call. There is WebClient in cxf which directly takes list but i am stuck with httpclient 4.x
|

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.