I am trying to upload a csv file to my endpoint. I tried doing this and resulting in 302 Found. Tried other solution mentioned here: Multipart POST Ruby HTTPS but same results.
First option using net/http
url = URI('https://abcd.com/test/upload')
out_file = '/Users/username/Downloads/test.csv'
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["username"] = 'abcd@test.com'
request["password"] = 'test2345'
form_data = [['file', File.open(out_file)]]
request.content_type = 'multipart/form-data'
request.set_form form_data, 'multipart/form-data'
response = https.request(request)
puts "Response from CRA is #{response.read_body}"
result:
#<Net::HTTPFound 302 Found readbody=true>
Second option using 'rest-client' with the steps mentioned in https://github.com/rest-client/rest-client
out_file = '/Users/username/Downloads/test.csv'
url = "https://abcd.com/test/upload"
begin
response = RestClient.post(url,:file => File.new(out_file, 'rb'),:headers => { "username": "abcd@test.com", "password": "test2345"})
rescue RestClient::ExceptionWithResponse => err
end
result:
[319] pry(main)> err
=> #<RestClient::Found: 302 Found>
[320] pry(main)> err.response
=> <RestClient::Response 302 "<html><head...">
[321] pry(main)> err.response.follow_redirection
IOError: closed stream
[322] pry(main)>
response body:
"<html><head><title>Object moved</title></head><body>\r\n<h2>Object moved to <a href=\"/Account/Login?ReturnUrl=%2ftesy%2fuploads\">here</a>.</h2>\r\n</body></html>\r\n",
url=#<URI::HTTPS https://abcd.com/test/upload>
Can anyone help me understand what am I doing wrong?