0

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?

Esh Val
  • 3
  • 3
  • seems like the URL you are posting to is returning a 302 redirect response. Can you provide the response body? – 23tux Feb 22 '21 at 15:44
  • @23tux Added response body to the description – Esh Val Feb 22 '21 at 16:59
  • It seems like you're trying to access an endpoint that needs some kind of authentication. That's why the response is a 302 redirect – 23tux Feb 22 '21 at 18:10

0 Answers0