I am using the ruby native library to do multipart/form-data API POST call. In this call, I am sending both json and files, but from the server side, the file is not uploaded correctly. Sometimes, it is uploaded successfully.
boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
url = URI("http://localhost:3000/pdfs")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request["authorization"] = "Bearer ddghfgdjh54544fdgdfghj"
request["accept"] = 'application/json'
request["content-type"] = "multipart/form-data; boundary=#{boundary}"
request["cache-control"] = 'no-cache'
body = []
# JSON data
body << "--#{boundary}\r\nContent-Disposition: form-data;"
body << "name=\"profile\"\r\n\r\n"
body << {user: {name:"xyz",email:"xyz@gmail.com"} }.to_json
body << "\r\n"
#File data
body << "--#{boundary}\r\n"
body << "Content-Disposition: form-data;"
body << "name=\"profile\"; filename=\"#{username}.pdf\"\r\nContent-Type: application/pdf\r\n"
body << "#{File.read('/home/pdfs/profile.pdf')}\r\n"
request.body = body.join
response = http.request(request)