如何使用 curl 在 Dspace 6 REST API 中上传比特流

How upload a bitstream in Dspace 6 REST API using curl

我正在尝试使用 curl 使用 DSpace 6 的 REST API 上传文件:

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'

发送正确,这是响应:

{"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"}

但是当我尝试访问上传的(Inside DSpace)文件时,它说它无效。 尝试上传一个纯文本文件并将这些添加到文件的顶部和末尾:

----------------------------9406e94bc5f35740 Content-Disposition:表单数据;姓名="upload";文件名="data.txt" 内容类型:text/plain

[文件内容在这里]

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

我尝试在 nodejs 中请求,但我得到了同样的错误。 一些帮助 ?提前致谢。

我在 curl 命令中使用 -T(上传文件)参数解决了这个问题:

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'

在 nodejs 中使用请求承诺:

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

对于 python 我的解决方案不是使用 files= ,而是使用 data= 以避免请求 multipart。

import io
params = {'name': 'file.csv'}
data = io.BytesIO(open('file.csv', 'rb').read())
res = session.post(url, data=data, params=params, headers={})