Problems with uploading files using REST API.

202 views
Skip to first unread message

gıyaseddin tanrıkulu

unread,
Mar 6, 2020, 9:26:17 AM3/6/20
to DSpace Technical Support
Hi there,
I am trying to upload files using REST API but for some reason, when I download these files, I see
a content-disposition header along with a number that is on top and bottom. Here's an example:


--3920b9ce37f4e3292af6d8081aa02501                                                                 
1 Content-Disposition: form-data; name="file"; filename="TODO"

...file content...

--3920b9ce37f4e3292af6d8081aa02501--

I tried to send this with and without header, but regardless the outcome is same. I suspect that upon saving
these files, DSpace mistakenly adds this header to file content.

I tried to upload using python and bash, I'm adding the code snippets in case if they provide any useful information.

Python:
PARAMS = {'Accept': "application/json",
         
'Content-Type': "application/json"}

def upload_file(file_path, item_uuid, cookie):
   
""" Upload file to a DSpace item.

    Arguments:
    file      -- The path of file to upload.
    item_uuid -- The item UUID in the rest api.
    cookie    -- JSESSIONID for authentication.
    """


   
# Add JSESSIONID to request header
    parameters
= PARAMS
    parameters
["Cookie"] = "JSESSIONID=" + cookie
    parameters
["Content-Type"] = "multipart/form-data"

   
# Prepare files dict for request
    file
= open(file_path, 'rb')
    files
= {'file': file}

    response
= requests.post(url=URL + "/rest/items/" + item_uuid +"/bitstreams?name=" + os.path.basename(file.name),
                             files
=files, headers=parameters)
    pdb
.set_trace()

    file
.close()



Bash:
curl -k -v -4 --trace dspace.log --cookie "JSESSIONID=2C20ADE1EEC3EA7E225C32884A31EC59" -H "Accept: application/json" -X POST "http://localhost:8080/rest/items/46309f1d-c5fd-4142-80d7-3c850b5f955b/bitstreams?name=test.sh" -T test.sh



Here's the information that you might need:

OS:
Manjaro Linux 19.0.2

Tomcat:
Server version: Apache Tomcat/8.5.51
Server built:   Feb 5 2020 22:26:25 UTC
Server number:  8.5.51.0
OS Name:        Linux
OS Version:     5.4.23-1-MANJARO
Architecture:   amd64
JVM Version:    1.8.0_242-b08
JVM Vendor:     Oracle Corporation

Maven:
Apache Maven 3.6.3 (NON-CANONICAL_2019-11-27T20:26:29Z_root)
Maven home: /opt/maven
Java version: 1.8.0_242, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.23-1-manjaro", arch: "amd64", family: "unix"

Ant:
Apache Ant(TM) version 1.10.7 compiled on September 10 2019

DSpace:
DSpace version:  6.3
  SCM revision:  813800ce1736ec503fdcfbee4d86de836788f87c
    SCM branch:  UNKNOWN
            OS:  Linux(amd64) version 5.4.23-1-MANJARO
  Applications:
                REST at http://localhost:8080/xmlui
                XMLUI at http://localhost:8080/xmlui
                REST at http://localhost:8080/xmlui
                XMLUI at http://localhost:8080/xmlui
                REST at http://localhost:8080/xmlui
                XMLUI at http://localhost:8080/xmlui
     Discovery:  enabled.
           JRE:  Oracle Corporation version 1.8.0_242
   Ant version:  Apache Ant(TM) version 1.10.7 compiled on September 10 2019
 Maven version:  3.3.9
 DSpace home:  /dspace

Any help would be appreciated ^_^

Ayuka Phanuel

unread,
Mar 7, 2020, 4:48:20 AM3/7/20
to gıyaseddin tanrıkulu, DSpace Technical Support
This is what i use:
And is working fine.
with open(directory, 'rb') as f:
file_response = requests.post(rest_items_api_link + item_uuid + "/bitstreams",
files={file_name: f},
params={'name': file_name},
headers={'Content-Type': 'multipart/form-data'},
cookies={"JSESSIONID": "xxxxx"})
print(file_response.text)




--
All messages to this mailing list should adhere to the DuraSpace Code of Conduct: https://duraspace.org/about/policies/code-of-conduct/
---
You received this message because you are subscribed to the Google Groups "DSpace Technical Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dspace-tech...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dspace-tech/20fc0555-370d-4ab7-9957-f4575431f65c%40googlegroups.com.

gıyaseddin tanrıkulu

unread,
Mar 12, 2020, 10:04:44 AM3/12/20
to DSpace Technical Support
No luck, same problem.
What version of DSpace are you using?
To unsubscribe from this group and stop receiving emails from it, send an email to dspac...@googlegroups.com.

Ayuka Phanuel

unread,
Mar 12, 2020, 11:59:30 AM3/12/20
to gıyaseddin tanrıkulu, DSpace Technical Support
Python 3 , DSpace 6.3

To unsubscribe from this group and stop receiving emails from it, send an email to dspace-tech...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dspace-tech/3df138d2-0f3c-4fe8-a550-45998d0937fe%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages