Uploading a file using ruby Net:HTTP API to a remote apache server failing with 409 Conflict

扶醉桌前 提交于 2020-01-02 10:46:08

问题


Below is a program to upload a file from local file system to a remote Apache server.

The program ends with a 409 conflict error. Is there any advice what am I doing wrong? I turned DAV on in httpd.conf and gave all necessary permissions, but I still had no luck. I can post the httpd.conf should it be needed.

This is the code:

BOUNDARY = "AaB03xZZZZZZ11322321111XSDW"
uri = URI.parse("http://localhost/dropbox/")
file = "/tmp/KEYS.txt"
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.request_uri)
request.body_stream=File.open(file)
request["Content-Type"] = "multipart/form-data"
request.add_field('Content-Length', File.size(file))
request.add_field('session', BOUNDARY)
response=http.request(request)
puts "Request Headers: #{request.to_hash.inspect}"
puts "Sending PUT #{uri.request_uri} to #{uri.host}:#{uri.port}"
puts "Response #{response.code} #{response.message}"
puts "#{response.body}"
puts "Headers: #{response.to_hash.inspect}"

And its output:

Request Headers: {"accept"=>["*/*"], "host"=>["localhost"], "content-length"=>[88873],     "content-type"=>["multipart/form-data"], "session"=>["AaB03xZZZZZZ11322321111XSDW"],   "connection"=>["close"]}
Sending PUT /dropbox/ to localhost:80
Response 409 Conflict
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>409 Conflict</title>
</head><body>
<h1>Conflict</h1>
<p>Cannot PUT to a collection.</p>
<hr />
<address>Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.10 with Suhosin-Patch Server at localhost Port 80</address>
</body></html>
Headers: {"server"=>["Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2  PHP/5.3.10 with Suhosin-Patch"], "content-length"=>["315"], "content-type"=>["text/html;  charset=ISO-8859-1"], "date"=>["Thu, 23 Aug 2012 17:36:40 GMT"], "connection"=>["close"]}

回答1:


The problem was resolved when I changed line 5 to this:

request = Net::HTTP::Put.new("#{uri.request_uri}/test.file")

The error "cannot PUT to a collection" means, an upload cannot be done against a folder. A file name must be specified.



来源:https://stackoverflow.com/questions/12097491/uploading-a-file-using-ruby-nethttp-api-to-a-remote-apache-server-failing-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!