4

I am trying to add a new bitstream file to a DSpace (version 5.2) item using rest call. I am making the rest call through a java program. I was able to successfully login to the REST API through my program. Here's my code segment:

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

But, I am confused about how to post a bitstream using REST calls. The DSpace REST Documentation does not clearly specify how to post the bitstream to DSpace. I have an image file which I want to add to an item(item id is known to me). According to the documentation:

POST /items/{item id}/bitstreams - Add bitstream to item. You must post a Bitstream

How can I post my image file in form of a Bitstream? For example, for logging in the REST API expects the email and password in a JSON array. In which format does the API expect the Bitstream.

Hope someone can help.

This is what I have tried:

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

The response returned by 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>
Poonam Anthony
  • 1,848
  • 3
  • 17
  • 27

2 Answers2

4

After a lot of attempts, I succeeded post bitstream file using Dspace rest API:

1- URL should contain name and optional description "with no space"
ex: http://domain-name.com:8080/rest/items/120/bitstreams?name=my_image.jpg&description=my_description

2- Header should contain:
- "rest-dspace-token" with value of login token.
- "Content-Type" can be "multipart/form-data" or "text/plain"

3- Posted content should be binary of the file without any name or key, like text plain.

example but using 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;
Belal mazlom
  • 1,790
  • 20
  • 25
2

An example using curl:

  1. First authenticate with the DSpace 5 REST API as follows:

curl -X POST http://localhost:8080/rest/login -d '{"email":"me@email.com", "password":"s3cret"}' -v -H "Content-Type: application/json"

  1. Then include the token in the response body in the rest-dspace-token header when uploading the bitstream:

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"

Tom Desair
  • 166
  • 1
  • 4