Top Menu

Jump to content
Home
    Modules
      • Projects
      • Activity
      • Work packages
      • Gantt charts
      • Calendars
      • Team planners
      • Boards
      • News
    • Getting started
    • Introduction video
      Welcome to OpenProject Community
      Get a quick overview of project management and team collaboration with OpenProject. You can restart this video from the help menu.

    • Help and support
    • Upgrade to Enterprise edition
    • User guides
    • Videos
    • Shortcuts
    • Community forum
    • Enterprise support

    • Additional resources
    • Data privacy and security policy
    • Digital accessibility (DE)
    • OpenProject website
    • Security alerts / Newsletter
    • OpenProject blog
    • Release notes
    • Report a bug
    • Development roadmap
    • Add and edit translations
    • API documentation
  • Sign in
      Forgot your password?

      or sign in with your existing account

      Google

Side Menu

  • Overview
  • Activity
    Activity
  • Roadmap
  • Work packages
    Work packages
  • Gantt charts
    Gantt charts
  • Calendars
    Calendars
  • Team planners
    Team planners
  • Boards
    Boards
  • News
  • Forums

Content

Development
  1. OpenProject
  2. Forums
  3. Development
  4. "Add attachment to work package" API Endpoint - difficulties

"Add attachment to work package" API Endpoint - difficulties

Added by R B over 2 years ago

https://www.openproject.org/docs/api/endpoints/attachments/
"/api/v3/work_packages/{id}/attachments

To add an attachment to a work package, a client needs to issue a request of type multipart/form-data with exactly two parts.

The first part must be called metadata. Its content type is expected to be application/json, the body must be a single JSON object, containing at least the fileName and optionally the attachments description.

The second part must be called file, its content type should match the mime type of the file. The body must be the raw content of the file. Note that a filename must be indicated in the Content-Disposition of this part, however it will be ignored. Instead the fileName inside the JSON of the metadata part will be used."

I have tried coding a solution to this, I've tried a mock in Postman. 

Possibly this is a language barrier, however, I nonetheless remain at a loss on how to upload a file and attach it to a workpackage via the API. 
In postman, the response I get is simply a "400 bad request":

https://***/api/v3/work_packages/23921/attachments
"form-data" checked. 

The below key value pairs: 

Key:                     Value: 

metadata            {"fileName":"test.png"}

file                       file.png

I would very much appreciate anyone's time in pointing me in the right direction. 

My eventual plan is to implement in Axios, but with a workable mock, I assume I can resolve this...


Replies (5)

RE: "Add attachment to work package" API Endpoint - difficulties - Added by Wheat Zhang over 1 year ago

from requests_toolbelt.multipart.encoder import MultipartEncoder

import requests

import json



# data

filename = "test.xlsx"

filepath = "/download/test.xlsx"

content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

multipart_encoder = MultipartEncoder(

    fields={

        'metadata': json.dumps({"fileName": filename}),

        'file': (filename, open(filepath, 'rb').read(), content_type)

        }

)



# ul

upload_files_url = "https://xxx/api/v3/work_packages/{id}/attachments"



# header

encoded_api_key = "xxxx"

file_headers = {

    'Authorization': f'Basic {encoded_api_key}'

}

file_headers['Content-Type'] = multipart_encoder.content_type



# request

response = requests.post(upload_files_url, headers=file_headers, data=multipart_encoder)

RE: "Add attachment to work package" API Endpoint - difficulties - Added by Markus Kahl over 1 year ago

This may come a little bit late, but there is a fairly straight-forward way to do this using bash. We link to a script for this in our docs.
But here is the essence of it:

FILE_NAME=`basename $FILE_PATH`
CONTENT_TYPE=`file --mime-type $FILE_PATH | cut -d: -f2 | tr -d ' '`

curl "https://$DOMAIN/api/v3/work_packages/$WP_ID/attachments" \
  -u "apikey:$API_KEY" \
  -H 'accept: application/json, text/plain, */*' \
  -F "metadata={\"fileName\":\"$FILE_NAME\",\"contentType\":\"$CONTENT_TYPE\"}" \
  -F "file=@$FILE_PATH"

R B wrote:

https://www.openproject.org/docs/api/endpoints/attachments/
"/api/v3/work_packages/{id}/attachments

To add an attachment to a work package, a client needs to issue a request of type multipart/form-data with exactly two parts.

The first part must be called metadata. Its content type is expected to be application/json, the body must be a single JSON object, containing at least the fileName and optionally the attachments description.

The second part must be called file, its content type should match the mime type of the file. The body must be the raw content of the file. Note that a filename must be indicated in the Content-Disposition of this part, however it will be ignored. Instead the fileName inside the JSON of the metadata part will be used."

I have tried coding a solution to this, I've tried a mock in Postman.

Possibly this is a language barrier, however, I nonetheless remain at a loss on how to upload a file and attach it to a workpackage via the API. 
In postman, the response I get is simply a "400 bad request":

https://***/api/v3/work_packages/23921/attachments
"form-data" checked.

The below key value pairs:

Key:                     Value:

metadata            {"fileName":"test.png"}

file                       file.png

I would very much appreciate anyone's time in pointing me in the right direction.

My eventual plan is to implement in Axios, but with a workable mock, I assume I can resolve this...

RE: "Add attachment to work package" API Endpoint - difficulties - Added by norma normal about 1 year ago

any one have solution in nodejs?

RE: "Add attachment to work package" API Endpoint - difficulties - Added by Abhijit Sahay 10 months ago

Hello:

I have been struggling with this as well.  The API call seems to succeed but doesn't actually upload my file as an attachment; instead, it returns the count of current attachments in the work package.  I would appreciate any help or pointers on this issue.

ps. I also tried Markus Kahl's suggestion above (CURL) but that doesn't work either.  Perhaps the script he refers to (https://www.openproject.org/docs/enterprise-guide/enterprise-cloud-guide/enterprise-cloud-faq/op-file-upload.sh) should be updated as well. 

RE: "Add attachment to work package" API Endpoint - difficulties - Added by Abhijit Sahay 10 months ago

Actually, I was able to make it work using Curl just by leaving out the header:

curl "https:myOpenProject/api/v3/work_packages/168/attachments" -u "xxxxx" -F 'metadata = {"fileName" : "foo.txt"}' -F 'file=@foo.txt'

  • (1 - 5/5)
Loading...