Ivan T
Ivan T

Reputation: 1046

cURL emulating by HTTPClient Apache

I'm trying to upload data to CrowdFlower using their API, for which I'm writing JAVA wrapper. I'm using HttpClient Apache.

The cURL example CrowdFlower give is following: curl -T 'sampledata.xlsx' -H 'Content-Type: application/vnd.ms-excel' https://api.crowdflower.com/v1/jobs/upload.json?key={api_key}

Here is my code:

public InputStream HTTPmethodPostUpload (String authKey, File file) throws ClientProtocolException, IOException{

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("https://api.crowdflower.com/v1/jobs/upload.json?key="+authKey);

        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbody = new FileBody( file,"application/vnd.ms-excel");
        mpEntity.addPart("sampledata.xlsx", cbody );
        httpPost.setEntity(mpEntity);
        HttpResponse response = httpclient.execute(httpPost);
        HttpEntity entityResponse = response.getEntity();
        return  entityResponse.getContent(); }

However it returns me an error with the following message:

{Un-Acceptable format, Content-Type must be one of those listed in "formats" but you sent "multipart/form-data; boundary=yTuwTm4hWmnasxIMB9dC-sxdELIGoNJVudjJdCz"","formats":["application/vnd.oasis.opendocument.spreadsheet","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.ms-excel","text/csv","text/plain"]}}

I don't know Apache HttpClient well so I don't understand where is the problem in my code.

Upvotes: 0

Views: 876

Answers (1)

Robert Rowntree
Robert Rowntree

Reputation: 6289

where in the java did you set the header?

-H 'Content-Type: application/vnd.ms-excel'

try the below:

mpEntity.setContentType("application/vnd.ms-excel");

Upvotes: 1

Related Questions