Skip to content

Instantly share code, notes, and snippets.

@alanorth
Last active November 27, 2023 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alanorth/40fc3092aefd78f978cca00e8abeeb7a to your computer and use it in GitHub Desktop.
Save alanorth/40fc3092aefd78f978cca00e8abeeb7a to your computer and use it in GitHub Desktop.
POSTing items to the DSpace 6 REST API

POSTing an item to /collections

First log in to get the JSESSIONID cookie and then post the item (I'm using httpie instead of curl):

$ http -f POST https://dspacetest.cgiar.org/rest/login email=aorth@mjanja.ch password=fuuuu
$ http https://dspacetest.cgiar.org/rest/status Cookie:JSESSIONID=EABAC9EFF942028AA52DFDA16DBCAFDE
$ http POST https://dspacetest.cgiar.org/rest/collections/f10ad667-2746-4705-8b16-4439abe61d22/items Cookie:JSESSIONID=EABAC9EFF942028AA52DFDA16DBCAFDE < item-object.json
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 07 Oct 2020 10:50:58 GMT
Server: nginx
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Robots-Tag: none
X-XSS-Protection: 1; mode=block

{
    "archived": "false",
    "bitstreams": null,
    "expand": [
        "metadata",
        "parentCollection",
        "parentCollectionList",
        "parentCommunityList",
        "bitstreams",
        "all"
    ],
    "handle": null,
    "lastModified": "Wed Oct 07 12:50:58 CEST 2020",
    "link": "/rest/items/335530b9-ed0e-4616-b81b-25cd3f14f9e9",
    "metadata": null,
    "name": "Testing REST API post",
    "parentCollection": null,
    "parentCollectionList": null,
    "parentCommunityList": null,
    "type": "item",
    "uuid": "335530b9-ed0e-4616-b81b-25cd3f14f9e9",
    "withdrawn": "false"
}

The response indicates that a new item has been created, and you get the internal UUID, but no Handle, because it hasn't been "archived" into the repository yet. If you post to a collection without a workflow then it will be archived automatically and you will get a Handle.

The item-object.json file looks like this:

{ "metadata": [
    {
      "key": "dc.title",
      "value": "Testing REST API post",
      "language": "en_US"
    },
    {
      "key": "dc.contributor.author",
      "value": "Orth, Alan",
      "language": "en_US"
    },
    {
      "key": "dc.date.issued",
      "value": "2020-09-01",
      "language": "en_US"
    }
  ],
  "archived":"false",
  "withdrawn":"false"
}

The archived and withdrawn keys seem to only be used for GETing items, not POSTing.

@abubelinha
Copy link

abubelinha commented Nov 27, 2023

Thanks for sharing! I didn't know httpie

Unfortunately I can't make the first line work on my institution DSpace server (same version 6.3 reported by https://dspacetest.cgiar.org/rest/status)
I am not receiving back JSESSIONID cookie value.
I get a 401 unauthorized message instead (despite I can login using the web interface using the same email/password).

Just to be sure it is not some SSL configuration problem in my workstation, would it be possible to use a test account in your 6.3 demo server? (or any other demo server that you know for 6.3 version)
I tried to create a test account in yours, to test your script there ... but I am not receiving the email for confirmation.

Thanks
@abubelinha

@abubelinha
Copy link

abubelinha commented Nov 27, 2023

Perhaps my institution DSpace server 6.3 has a working /rest endpoint, but only for non authenticated requests? (so rest authentication is disabled by admins). I don't know if that is ever possible. This is what happens:

  • I have successfully logged and uploaded a paper to the collection I belong to, using the normal web interface.
  • I have also been able to read the same collection contents using /rest/collections/{collectionUUID}/items
  • But I am not being able to login to the server using any of the curl/python/httpie examples I found. Always 401 response

I guess this is the correct documentation for that version: https://wiki.lyrasis.org/display/DSDOC6x/REST+API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment