如何使用httparty上传视频文件

发布于 2024-10-19 23:13:56 字数 57 浏览 2 评论 0原文

嗨,我看过 httparty,但我不知道如何实现,有很多模型文件的示例,但哪些参数可用于上传视频文件

hi I have seen httparty but I don't have a idea to how to implement there are many example of model file but which paramater can be use for upload the video file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

楠木可依 2024-10-26 23:13:57

HTTParty 似乎不支持文件上传(至少从 2009 年开始):

Google 群组中有人说不支持:
http://groups.google.com/group/httparty-gem/browse_thread /thread/fe8a3af8c46e7c75

与之相关的开放问题:
https://github.com/jnunemaker/httparty/issues/77

HTTParty does not appear to support file uploads (as least as of 2009):

Google groups where some people say it's not supported:
http://groups.google.com/group/httparty-gem/browse_thread/thread/fe8a3af8c46e7c75

Open Issue concerning it:
https://github.com/jnunemaker/httparty/issues/77

寂寞笑我太脆弱 2024-10-26 23:13:57

通过使用 RestClient gem,我们可以轻松处理文件上传,但请确保安装了 Rest-client gem。

我还附上了邮递员的示例请求的屏幕截图。

控制器中的代码如下所示,

  res = RestClient::Request.execute(
      method: :post,
      url: "http://www.your_url.com",
      payload: params,

      headers: {
          'Content-Type' => "multipart/form-data",
          Authorization: "some_key",
          :Accept => "*/*"
      }

  )

在此处输入图像描述

By using the RestClient gem we can easily handle the file uploads, but make sure that you install the rest-client gem.

And I have also attached screen shot of example request from postman.

And the code in controller as like below,

  res = RestClient::Request.execute(
      method: :post,
      url: "http://www.your_url.com",
      payload: params,

      headers: {
          'Content-Type' => "multipart/form-data",
          Authorization: "some_key",
          :Accept => "*/*"
      }

  )

enter image description here

纵情客 2024-10-26 23:13:57

这是来自 HTTParty 的多部分示例:

# If you are uploading file in params, multipart will used as content-type automatically

HTTParty.post(
  'http://localhost:3000/user',
  body: {
    name: 'Foo Bar',
    email: '[email protected]',
    avatar: File.open('/full/path/to/avatar.jpg')
  }
)

现在,您可以在控制器中访问上传的文件,如下所示:

uploaded_io = params[:avatar]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
  file.write(uploaded_io.read)
end 

Here is the multipart example from HTTParty:

# If you are uploading file in params, multipart will used as content-type automatically

HTTParty.post(
  'http://localhost:3000/user',
  body: {
    name: 'Foo Bar',
    email: '[email protected]',
    avatar: File.open('/full/path/to/avatar.jpg')
  }
)

Now, you can access the uploaded file in the controller as follows:

uploaded_io = params[:avatar]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
  file.write(uploaded_io.read)
end 
╄→承喏 2024-10-26 23:13:56

HTTMultiParty 通过发布多部分 MIME 文档来支持文件上传。它包装了 HTTParty,因此您可以以相同的方式使用它。对于文件,只需提供一个 File 对象。自述文件中的示例:

require 'httmultiparty'
class SomeClient
  include HTTMultiParty
  base_uri 'http://localhost:3000'
end

response = SomeClient.post('/', :query => {
  :foo      => 'bar',
  :somefile => File.new('README.md')
})

HTTMultiParty supports file uploads by posting a multipart MIME document. It wraps HTTParty so you use it in the same way. For the file, just supply a File object. Example from the README:

require 'httmultiparty'
class SomeClient
  include HTTMultiParty
  base_uri 'http://localhost:3000'
end

response = SomeClient.post('/', :query => {
  :foo      => 'bar',
  :somefile => File.new('README.md')
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文