0
votes

At the beginning I can upload my file with this curl request :

curl -u "admin:admin" -X POST "http://myurlu:9086/service/rest/v1/components?repository=ebooks-store" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "raw.directory=test" -F "[email protected];type=application/pdf" -F "raw.asset1.filename=billet.pdf"

And with the successful log :

2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Uploading component with parameters: repository="ebooks-store" format="raw" directory="test" 2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters: file="billet.pdf" filename="billet.pdf"

I have the use case to upload in my code, so I created this service using jersey :

FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:\\Users\\tkossoko\\Documents\\article.pdf"), 
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
                .field("raw.directory", "test")
                .field("raw.asset1.filename.", "article.pdf")
                .bodyPart(filePart);

        String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
        webTarget = client.target(url);
        //Very important to do, we have to register this
        webTarget.register(MultiPartFeature.class);
        final Response response = webTarget.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).header("Expect", "100-continue")
                .post(Entity.entity(multipart, multipart.getMediaType()));

        //Use response object to verify upload success

        formDataMultiPart.close();
        multipart.close();

But I got each time this error :

2019-08-27 17:28:29,617+0000 INFO [qtp969575574-10306] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Uploading component with parameters: repository="ebooks-store" format="raw" asset1.filename.="article.pdf" directory="test" 2019-08-27 17:28:29,618+0000 INFO [qtp969575574-10306] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters: file="article.pdf" 2019-08-27 17:28:29,619+0000 WARN [qtp969575574-10306] admin org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID c6a95618-aed1-45e8-90ee-2af315eb209b) Response: [400] '[ValidationErrorXO{id='asset1.filename.', message='Unknown component field 'asset1.filename.''}, ValidationErrorXO{id='filename', message='Missing required asset field 'Filename' on '1''}]'; mapped from: org.sonatype.nexus.rest.ValidationErrorsException: Unknown component field 'asset1.filename.', Missing required asset field 'Filename' on '1'

What is wrong in my code ? The webservice I use is waiting for 3 paramaters :

raw.directory String raw.assetN File raw.assetN.filename String

1
The file part you are sending with the name "file". In your curl code, the file is being send with the part name raw.asset1. The first argument to the FileDataBodyPart constructor is the name of the part.Paul Samsotha
@PaulSamsotha thanks its work perfectlyTeddy Kossoko

1 Answers

0
votes

the correct answer is

FileDataBodyPart filePart = new FileDataBodyPart("raw.asset1", new File("C:\\Users\\tkos\\Documents\\article.pdf"), 
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
                .field("raw.directory", "test")
                .field("raw.asset1.filename.", "article.pdf")
                .bodyPart(filePart);

        String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
        webTarget = client.target(url);
        //Very important to do, we have to register this
        webTarget.register(MultiPartFeature.class);
        final Response response = webTarget.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).header("Expect", "100-continue")
                .post(Entity.entity(multipart, multipart.getMediaType()));

        //Use response object to verify upload success

        formDataMultiPart.close();
        multipart.close();