restcurldspace

How upload a bitstream in Dspace 6 REST API using curl


I am trying to upload a file using the REST API of DSpace 6 using curl:

curl -k -4 \
    -H "Content-Type: multipart/form-data" \
    --cookie "JSESSIONID=E7B87CCFA35FB83670F379072505580E" \
    -H "accept: application/json" \
    -X POST "http://localhost:5553/rest/items/4f7b0dba-428d-458a-854d-141350b9b678/bitstreams?name=Picture.jpg" \
    -F 'upload=@picture.jpg'

Sending is done correctly, this is the response:

{"uuid":"737b78b7-8369-4b47-a36a-69ddd7f24bda","name":"Picture.jpg","handle":null,"type":"bitstream","expand":["parent","policies","all"],"bundleName":"ORIGINAL","description":null,"format":"JPEG","mimeType":"image/jpeg","sizeBytes":570897,"parentObject":null,"retrieveLink":"/rest/bitstreams/737b78b7-8369-4b47-a36a-69ddd7f24bda/retrieve","checkSum":{"value":"0dec466b8d8546a60f39882f7735f084","checkSumAlgorithm":"MD5"},"sequenceId":-1,"policies":null,"link":"/rest/bitstreams/737b78b7-8369-4b47-a36a-69ddd7f24bda"}

But when I try to access the uploaded (Inside DSpace) file it says it is not valid. Try upload a plain text file and add these to the top and to the end of the file:

--------------------------9406e94bc5f35740 Content-Disposition: form-data; name="upload"; filename="data.txt" Content-Type: text/plain

[The file content here]

--------------------------9406e94bc5f35740--

I try with request in nodejs but i get the same error. Some help ? Thanks in advance.


Solution

  • I solved it using the -T (upload-file) param in the curl command:

    curl -k -4 -v \
        -H "Content-Type: multipart/form-data" \
        --cookie "JSESSIONID=E7B87CCFA35FB83670F379072505580E" \
        -H "accept: application/json" \
        -X POST "http://localhost:5553/rest/items/4f7b0dba-428d-458a-854d-141350b9b678/bitstreams?name=picture.jpg" \
        -T 'picture.jpg'
    

    In nodejs using request-promise:

    const rp = require('request-promise')
    const fs = require('fs');
    const path = require('path');
    
    const PostBitstream = async (itemId) => {
    
        try {
    
            const bitstream = fs.createReadStream(path.join('files', 'picture.jpg'));
    
            const params =  {
                method: 'POST',
                uri: `${BASE_URL}/items/${itemId}/bitstreams?name=${FILE_NAME}`,
                headers: { "Content-Type": "multipart/form-data", "accept": "application/json" },
                encoding: null,
                body: bitstream
            }
    
            const res = await rp(params);
            console.log(res);
        } catch (err) {
    
            console.log(err.stack);
        }
    }