Feature #8171

Adding attachments through the REST API

Added by Robert Gstöhl about 1 year ago. Updated 2 months ago.

Status:Closed Start date:2011-04-17
Priority:Normal Due date:
Assignee:Jean-Philippe Lang % Done:

0%

Category:REST API
Target version:1.4.0
Resolution:Fixed

Description

This seems to be requested often:

http://www.redmine.org/issues/4968
https://www.chiliproject.org/issues/166

Currently people are working around this limitation in various ways (adding attachments by mail, constructing multipart form requests, ...).

I just started learning rails and the redmine internals, but wouldn't it be possible to just pass the attachment as base64 encoded string inside the issue, -json / xml?

On the issue controller side, something like this might work (it did work for me while testing things out with curl):

 1diff --git a/app/models/attachment.rb b/app/models/attachment.rb
 2index 99e461d..e358785 100644
 3--- a/app/models/attachment.rb
 4+++ b/app/models/attachment.rb
 5@@ -144,6 +144,18 @@ class Attachment < ActiveRecord::Base
 6     if attachments && attachments.is_a?(Hash)
 7       attachments.each_value do |attachment|
 8         file = attachment['file']
 9+        if file.is_a?(Hash)
10+          #Looks like we got a base64 encoded file from the api
11+          require "base64" 
12+          require "tempfile" 
13+          base64encodedData = file["data"]
14+          tempfile = Tempfile.new("redmine_rest_api_attachment")
15+          tempfile.content_type = file["content_type"]
16+          tempfile.original_filename = file["original_filename"]
17+          tempfile.write(Base64.decode64(file["data"]))
18+          tempfile.rewind
19+          file = tempfile
20+        end
21         next unless file && file.size > 0
22         a = Attachment.create(:container => obj, 
23                               :file => file,
 1diff --git a/config/initializers/tempfile_for_attachments.rb b/config/initializers/tempfile_for_attachments.rb
 2new file mode 100644
 3index 0000000..779140e
 4--- /dev/null
 5+++ b/config/initializers/tempfile_for_attachments.rb
 6@@ -0,0 +1,4 @@
 7+require 'tempfile'
 8+class Tempfile
 9+   attr_accessor :content_type, :original_filename
10+end

Not pretty but kind of works. If someone could help me out a bit, I guess we could implement something much cleaner pretty quickly.

Bonus points if the thing works out of the box with active resource ;)


Related issues

related to Feature #4968: REST API new features Closed 2010-03-02

Associated revisions

Revision 8928
Added by Jean-Philippe Lang 3 months ago

Adds support for adding attachments to issues through the REST API (#8171).

History

#1 Updated by Mischa The Evil about 1 year ago

  • Category set to REST API

Fixed description and category.

#2 Updated by Etienne Massip about 1 year ago

  • Target version set to Candidate for next major release

#3 Updated by Terence Mill 8 months ago

Can u provide an patch for redmine 1.2.1 and a curl example how you test the attachment upload?

Tx for help!

+1 for this feature

#4 Updated by Bruno Medeiros 6 months ago

Part of this seems to be already done, as we have this page: http://www.redmine.org/projects/redmine/wiki/Rest_Attachments

Could someone update the summary/%done to match the current state?

#5 Updated by Emmanuel Bourgerie 5 months ago

Hi !

I am in need of such a feature, I'd be glad to test any patch :)
Is there anything functional yet ?

#6 Updated by Alexey Skor 5 months ago

several people have asked me to add "upload attachment" to Redmine Java API. I'll have to track this REST API request. Can't do anything in the Java API until it's supported by REST.

#7 Updated by Hannes Meier 5 months ago

I am new to redmine and programming but just tested the rest api by using redminews to generate ticktes. It worked pretty easy but i wanted to go a step ahead now and attach files to the tickets. Then i found this open ticket here.
In my case a kind of "softimport" would be enough, just to give the creator a path information (data in same network like redmine installation) and redmine would connect (link to) this picture to a ticket/issue.
That would be so awesome.
But even without this feature redmine is a fantastic project! thank you for this great piece of software!

#8 Updated by Hannes Meier 5 months ago

I am stupid ... I guess this can be done in the description using html ;D or the defined tags. I ll test it but im sure it ll work. sorry

#9 Updated by Hannes Meier 5 months ago

Well it works with real urls like http://www.arealwebpage.com/image.jpg but not with local files. This seems to be a security issue, or?
So a possibility to attach files to an issue would still be great.
Or is there a way around and a possibilty to include local files in the issue description?

#10 Updated by Jean-Philippe Lang 3 months ago

  • Subject changed from Extend REST Api to support Attachments to Adding attachments through the REST API
  • Assignee set to Jean-Philippe Lang
  • Target version changed from Candidate for next major release to 1.4.0

#11 Updated by Jean-Philippe Lang 3 months ago

  • Status changed from New to Closed
  • Resolution set to Fixed

Feature added in r8928. See Attaching-files in the REST API documentation.

#12 Updated by Gert van Dijk 2 months ago

Feature #7725, about uploading files in the 'Files' section of a project seems related to this.

Also available in: Atom PDF