0

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

Hower 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.

szabgab
  • 6,202
  • 11
  • 50
  • 64
Ivan T
  • 1,046
  • 1
  • 10
  • 22

1 Answers1

1

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");
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
  • Unfortunatelly there is no such a method for MultipartEntity class. – Ivan T May 26 '13 at 11:10
  • 1
    The API should provide alternate access to HTTP HEADER. That is what you need to set. The example that i posted was via the Entity object. Try a different route to the headers. – Robert Rowntree May 26 '13 at 15:23