9

I'm afraid I've not got much experience with posting documents (e.g. XML) over web-servers, so I apologise if my understanding of HTTP is lacking.

I have a basic Mongrel web server set up in a ruby app on 127.0.0.1 port 2000. (The server).

I am running a separate Ruby app on the same computer. (The client).

I need the client to POST an XML document to the server.

I have tried using Net::HTTP to do this, but I can't find a clear example which tells me what I should do. I've had a go, but encountered errors. I have broken the request down to make it as basic as possible:

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

but it results in 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 imagine I'm doing it totally wrong. Please can you give me an example (or point me to a tutorial) that should allow me to post some basic data, like "<tag1>text</tag1>". Hopefully, I will then be able to work out setting the appropriate headers and handling the response.

Also, I don't need to use net/http; any free method that doesn't come with extra commercial-use licencing restrictions is fine.

2
  • You're attempting to debug the client. But have you proven that the server functions properly? Commented Feb 1, 2012 at 1:46
  • I access the same URL via a web browser and the server correctly does p on the request (I've not set it to do anything else yet). When connecting via the client, the server reports that it's receiving a malformed HTTP request HTTP parse error, malformed request (127.0.0.1): #<Mongrel::HttpParserError: Invalid HTTP format, parsing fails.>. I'm pretty sure that it's just that I don't know how to correctly post the data to the server. If I can find out how I should be doing it, I can debug from there, if necessary. Commented Feb 1, 2012 at 2:17

1 Answer 1

4

This is incredibly easy, 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"
Sign up to request clarification or add additional context in comments.

2 Comments

If you check the request server log, the parameters will be {"tag1"=>"text"} instead of plain "<tag1>text</tag1>". Why is that?
I don't know. If you want an answer, you should really ask that as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.