我正在使用ruby本機庫來執行multipart/form-data API POST調用。 在這個調用中,我發送了json和文件,但是從服務器端文件沒有正確上傳。有時候,它會成功上傳。通過Net :: Http在ruby上傳PDF文件到外部源
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:"[email protected]"} }.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)
怎麼做JSON API POST調用和文件作爲多/ form-data的 – santoshkumar