Using Ruby to send an XML request to a web server

I am afraid that I do not have much experience publishing documents (e.g. XML) through web servers, so I apologize if my understanding of HTTP is missing.

I have a basic Mongrel web server configured in a ruby โ€‹โ€‹application on 127.0.0.1 port 2000 . (Server).

I am running a standalone Ruby application on the same computer. (Client).

I need a client to send an XML document to the server.

I tried using Net :: HTTP to do this, but I cannot find a clear example that tells me what I should do. I had problems, but I ran into errors. I violated the request to make it as simple as possible:

 http = Net::HTTP.new("127.0.0.1", 2000) http.post('file', 'query=foo') #xc.rb line 6 

but this leads to the following error

  C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': An existing connection was forcibly closed by the remote host. (Errno::ECONNRESET) from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill' from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil' from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:132:in `readline' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2551:in `read_new' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `catch' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1293:in `request' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1286:in `block in request' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1284:in `request' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1307:in `send_entity' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1096:in `post' from W:/Ruby/A/xc.rb:6:in `<main>' 

I assume that I am doing this completely wrong. Please give me an example (or give me a tutorial) that will allow me to publish some basic data, such as "<tag1>text</tag1>" . Hopefully after that I can develop the appropriate headers and process the response.

In addition, I do not need to use net / http; any free method that does not provide additional licenses for commercial use is excellent.

+8
post ruby client
source share
1 answer

It is incredibly simple when using the rest-client gem

 require 'rest_client' response = RestClient.post "http://127.0.0.1:2000", "<tag1>text</tag1>", :content_type => "text/xml" 
+4
source share

All Articles