使用DSpace REST API

时间:2015-10-10 15:43:23

标签: rest dspace

我正在尝试使用rest调用将新的比特流文件添加到DSpace(版本5.2)项目。我正在通过java程序进行其余的调用。我能够通过我的程序成功登录REST API。这是我的代码段:

HttpPost post = new HttpPost(dspace_rest_url+"login");
StringEntity input = new StringEntity("{\"email\":\""+dspace_email+"\",\"password\":\""+dspace_password+"\"}"); 
input.setContentType("application/json");
post.setEntity(input);
HttpResponse response = client.execute(post);

但是,我对如何使用REST调用发布比特流感到困惑。 DSpace REST Documentation没有明确说明如何将比特流发布到DSpace。我有一个图像文件,我想添加到项目(项目ID是我知道的)。根据文件:

  

POST / items / {item id} / bitstreams - 将比特流添加到项目。您必须发布比特流

如何以比特流的形式发布我的图像文件?例如,对于登录REST API,需要JSON数组中的电子邮件和密码。 API期望比特流采用哪种格式。

希望有人可以提供帮助。

这就是我的尝试:

HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(dspace_rest_url+"items/"+itemID+"/bitstreams");

post.addHeader("rest-dspace-token", token);
File postFile = new File(thumbnailPath);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();        

builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody cbFile = new FileBody(postFile, "image/jpeg");
builder.addPart("userfile", cbFile);

HttpEntity entity = builder.build();
post.setEntity(entity);
System.out.println("executing request " + post.getRequestLine());
HttpResponse response = client.execute(post);

DSpace REST返回的响应:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bitstream>
<expand>parent</expand>
<expand>policies</expand>
<expand>all</expand>
<id>461945</id>
<type>bitstream</type>
<bundleName>ORIGINAL</bundleName>
<checkSum checkSumAlgorithm="MD5">d281b5cbf5d2001e266ed3252a50fb2d</checkSum>
<format>Unknown</format>
<mimeType>application/octet-stream</mimeType>
<retrieveLink>/bitstreams/461945/retrieve</retrieveLink>
<sequenceId>-1</sequenceId>
<sizeBytes>5677</sizeBytes>
</bitstream>

2 个答案:

答案 0 :(得分:4)

经过多次尝试,我成功使用Dspace rest API发布了比特流文件:

1- URL应包含名称和可选描述&#34;没有空格&#34;
例如:http://domain-name.com:8080/rest/items/120/bitstreams?name=my_image.jpg&description=my_description

2-标题应包含:
- &#34; rest-dspace-token&#34;具有登录令牌的值。
- &#34;内容类型&#34;可以是&#34; multipart / form-data&#34;或&#34; text / plain&#34;

3-发布的内容应该是文件的二进制文件,没有任何名称或密钥,例如text plain。

示例,但使用php curl:

$file_name_with_full_path = realpath('./my_image.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain-name.com:8080/rest/items/120/bitstreams?name=my_image.jpg&description=my_description');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Accept: application/json', 'rest-dspace-token: ' . $dspaceToken));
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_name_with_full_path));
$result = curl_exec($ch);
curl_close($ch);
echo $result;

答案 1 :(得分:1)

使用curl的示例:

  1. 首先使用DSpace 5 REST API进行身份验证,如下所示:
  2. curl -X POST http://localhost:8080/rest/login -d '{"email":"me@email.com", "password":"s3cret"}' -v -H "Content-Type: application/json"

    1. 然后在上传比特流时在rest-dspace-token标题的响应正文中包含令牌:
    2. curl -v -X POST -H "Content-Type: multipart/form-data" -H "rest-dspace-token: 5f042a2a-3a11-4833-b5bf-07c161272bdb" --data-binary @/path/to/test2.pdf "http://localhost:8080/rest/items/120/bitstreams?name=test2.pdf"