kikovalle / PLGSharepointRestAPI-java

Easy to use wrapper for the Sharepoint Rest API v1. Even if this is not a full implementation it covers most common use cases and provides examples to extending this API.
MIT License
42 stars 32 forks source link

Invalid mime type "application": does not contain '/' #30

Closed cpesch closed 3 years ago

cpesch commented 3 years ago

Dear @kikovalle,

I'm trying to download the bytes of a CSV file from a Sharepoint 2013 and I'm using PLGSharepointOnPremisesClient#downloadFile

I'm using the serverRelativeUrl which works when accessing it from the browser.

When using the call above, I receive this exception:

org.springframework.http.InvalidMediaTypeException: Invalid mime type "application": does not contain '/'
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:620)
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:992)
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:136)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:93)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1037)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1020)
    at com.panxoloto.sharepoint.rest.StreamRestTemplate$StreamResponseExtractor.extractData(StreamRestTemplate.java:163)
    at com.panxoloto.sharepoint.rest.StreamRestTemplate$StreamResponseExtractor.extractData(StreamRestTemplate.java:145)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:621)
    at com.panxoloto.sharepoint.rest.PLGSharepointOnPremisesClient.downloadFile(PLGSharepointOnPremisesClient.java:430)

I've tried to submit "Accept: application/octet-stream" or omit sending the Accept header like here https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest but this doesn't help.

Do you have experiences with that?

kikovalle commented 3 years ago

Hello

           PLGSharepointClient wrapper = new PLGSharepointClientOnline(user, passwd, domain, spSiteUrl);
    try {
        Resource result = wrapper.downloadFile("/sites/YOURSITE/LIBRARY/FILENAME.EXTENSION);
        System.out.println(result);
        InputStream is = new BufferedInputStream(result.getInputStream());
        OutputStream os = new BufferedOutputStream(new FileOutputStream("./test.EXTENSION"));
        IOUtils.copy(is, os);
        is.close();
        os.close();
        Assert.assertTrue(result != null );
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Assert.assertTrue(false);
    }

Can you try adapting this to a OnPremises instance adapting the download URL? This is extracted from a test i used to test the methods and to test changes. The file url must contain the the full uri of the file, including the /site/SITENAME where the library is located.

With online instance this is working

cpesch commented 3 years ago

Here are my findings:

  1. I've tried to add a header like described https://sharepoint.stackexchange.com/questions/273774/how-to-get-content-of-file-in-sharepoint-using-sharepoint-rest-api but this doesn't change the Content-Type header that Sharepoint returns. You can find the change here: https://github.com/cpesch/PLGSharepointRestAPI-java/commit/34c2a2ad2414533817eff641afa16114ef941a75

  2. I made a workaround and adjusted the Content-Type header if it has not subtype. This works but it's not nice: https://github.com/cpesch/PLGSharepointRestAPI-java/commit/e20ff6600ebbc745e1b78009ecd733f0dca16edb