Asana Api Rails附件 [英] Asana Api Rails Attachment

查看:114
本文介绍了Asana Api Rails附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向Asana任务添加附件.

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

我的JSON请求正文如下:

My JSON request body is as follows:

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

我在POST上收到以下输出:

I receive this output on the POST:

error: file: File is not an object 

"attachment"变量是常规的rails表单附件.

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

有什么想法吗?

-----编辑-----

-----EDIT-----

对于未来的任何人,我都使用法拉第(Faraday)宝石找到了答案.这是我使用的代码:

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 

推荐答案

嗯,实际上是您无法使用JSON的一种情况-您需要进行表单编码的上传,否则,您只是尝试设置字符串"@ tmpfile.txt"(或您拥有的内容)的文件"参数.

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).

我对Rails并不特别熟悉,但真正的问题是如何从Rails发布文件上传"-Asana端点在这里的工作方式与所有其他表单上传完全相同.

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.

这篇关于Asana Api Rails附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆