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>