Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to upload multiple files as form-data #830

Closed
chebyte opened this issue Oct 22, 2018 · 2 comments
Closed

how to upload multiple files as form-data #830

chebyte opened this issue Oct 22, 2018 · 2 comments
Labels
info Generic question on how to use Faraday

Comments

@chebyte
Copy link

chebyte commented Oct 22, 2018

hello there I need to upload using faraday the following example

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="listing"
{  
   "listing":{  
      "tickets":[  
         {  
            "row":"X",
            "seat":"201",
            "name":"file1"
         },

         {  
            "row":"X",
            "seat":"202",
            "name":"file2"
         }
      ]
   }
}
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file1"

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="file2"

------WebKitFormBoundary7MA4YWxkTrZu0gW--

how can be transcribe to faraday way?

thanks

@iMacTia
Copy link
Member

iMacTia commented Oct 23, 2018

Hi @chebyte,

I must confess I don't personally use the multipart upload middleware, however I'd try with the below:

json = {
  listing: {
    tickets: [
      {
        row: 'X',
        seat: '201',
        name: 'file1'
      },
      {
        row: 'X',
        seat: '202',
        name: 'file2'
      }
    ]
  }
}

payload = {
  listing: JSON.dump(json),
  file1: Faraday::UploadIO.new(file1_path, file1_content_type)
  file2: Faraday::UploadIO.new(file2_path, file2_content_type)
}

conn = Faraday.new(url) do |f|
  f.request :multipart
  f.request :json
  f.adapter :net_http
end

conn.post(path, payload)

There will be some variables you'll need to set which I'm not able to infer: url, path, file1_path, file1_content_type, file2_path, file2_content_type.
Please give it a try and let me know the result, we can then see from there how to improve to get what you need 👍

@iMacTia iMacTia added the info Generic question on how to use Faraday label Oct 23, 2018
@iMacTia iMacTia closed this as completed Nov 27, 2018
@bharam
Copy link

bharam commented Jan 5, 2020

I understand that this issue has been closed, but please help with the issue I am having which is close to this one.

Below is what I need to upload

-data-binary $'

------WebKitFormBoundaryInmulX6ait6ZMLdu\r\n
Content-Disposition: form-data; name="message"; filename="blob"\r\n
Content-Type: application/json
{
 "title":"title of the message to be sent",
 "body":"body of the message to be sent",
 "attachedFileName":null,
 "attachedFileName2":null,
 "attachedFileName3":null,
 "recipientIdList":[186554]
}
 ------WebKitFormBoundaryInmulX6ait6ZMLdu--\r\n' --compressed

Please note content type that the server receives is multipart/form-data

my current code looks like below:

client = Faraday::Connection.new(url: BASE_URL) do |builder|
    builder.use :cookie_jar
    builder.use :multipart
    builder.use :url_encoded
    builder.adapter :net_http
end

message = {title: "title", body: "body of message", recipientIdList:[186554]}
payload = {message: JSON.dump(message}

response = @client.post(URL) do |request|
    request.headers['Content-Type'] = 'multipart/form-data'
    request.body = payload
end

However, the response I get from the server is {"success"=>false, "errorCode"=>nil, "message"=>"Content type 'application/octet-stream' not supported", "data"=>nil}

I tweaked the code little by little and tried again, and searched google for hours but could not find solution.
It would be greatly appreciated if someone can help me with this.
Thank you in advance :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info Generic question on how to use Faraday
Projects
None yet
Development

No branches or pull requests

3 participants