Asana Api Rails Attachment

笑着哭i 提交于 2019-12-12 06:21:13

问题


I am attempting to add an attachment to an Asana task.

My JSON request body is as follows:

request_body = {
  "data" => {
    "file" => "@#{attachment.tempfile}"
  }
}

I receive this output on the POST:

error: file: File is not an object 

The "attachment" variable is a regular rails form attachment.

Any ideas?

-----EDIT-----

For anyone looking in the future, I figured it out using the Faraday gem. Here is the code I used:

connection = Faraday.new(:url => @uri) do |conn|
  conn.response :logger                
  conn.request :multipart
  conn.request :url_encoded
  conn.basic_auth(@api_key, '')
  conn.adapter :net_http 
end

payload = { :file => Faraday::UploadIO.new(file, file_type) }
response = connection.post(@uri, payload)
return response 

回答1:


Ah, that's actually the one case you can't use JSON - you need to do a form-encoded upload, otherwise you're just trying to set the "file" parameter to the string "@tmpfile.txt" (or what have you).

I'm not familiar with rails specifically but the real question here is "how to post a file upload from Rails" - the Asana endpoint here works exactly like all other form uploads.



来源:https://stackoverflow.com/questions/28733324/asana-api-rails-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!