All threads / Make http POST request that return xml response

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Make http POST request that return xml response

elian asked in Rails

I want to make a http POST request that parse XML response and return the value of SessionId field that is inside XML. This is what I tried so far but I want to return SessionId filed that arrives with the response and I don't know if this is the right approach to have a xml response.
Ps: is there a way I can run this class from the console, in the way that I can see the response?

class Documents::CreateSession

def initialize()
@username = Rails.secrets.legal_doc.username
@password= Rails.secrets.legal_doc.password
end

def start

require "net/http"
require "uri"

uri = URI.parse("http://example.com/search")

http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"userid" => @username, "password" => @password})

response = http.request(request)

end

end

You can run that code in the console by running the same code as you would in your app. response = Documents::CreateSession.new.start

The response will be the return value of start as it currently is setup.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,311+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.