Question

I am using Commons HttpClient to send a post request along with some string content as parameter. Following is my code:

    // obtain the default httpclient
    client = new DefaultHttpClient();

    // obtain a http post request object
    postRequest = new HttpPost(stanbolInstance);
    postRequest.setHeader("Accept", "application/json");

    // create an http param containing summary of article
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("data", content));

    try {
        // add the param to postRequest
        postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        // obtain the response
        response = client.execute(postRequest);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Here, stanbolInstance is: http://dev.iks-project.eu:8081/enhancer It does not work. Following is the exception:

Problem accessing /enhancer. Reason:
<pre>    The parsed byte array MUST NOT be NULL!</pre></p><h3>Caused by:</h3><pre>java.lang.IllegalArgumentException: The parsed byte array MUST NOT be NULL!

Following is the cURL equivalent which works:

curl -X POST -H "Accept: text/turtle" -H "Content-type: text/plain" --data "The Stanbol enhancer can detect famous cities such as Paris and people such as Bob Marley." http://dev.iks-project.eu:8081/enhancer

Help!

Was it helpful?

Solution

I think you're putting the content in the wrong way.

Replace:

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("data", content));

    try {
        // add the param to postRequest
        postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

with

postRequest.setEntity(new StringEntity(content));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top