rostov-7
rostov-7

Reputation: 25

Net::HTTP send request xml

I need to send the request in the form of xml.

uri =URI('https://fs.example.com:8443/services/trust/13/usermixed')
http = Net::HTTP.new(uri)
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/soap+xml; charset=UTF-8'
request.body = @body(as xml)
response = http.request(request)

When sending a request to the uri at the end of the method Net::HTTP::Post.new hooks port 80, as a result it turns out a connection on https://fs.example.com:8443/services/trust/13/usermixed:80 which leads to error SocketError: Failed to open TCP connection to https://fs.example.com:8443/services/trust/13/usermixed:80 (getaddrinfo: Name or service not known)

А similar problem is solved by the method Net::HTTP.post_form(uri, data) no ports at the end ... but it takes a date only by hash. I need to send request.body to XML

Thanks!

Upvotes: 2

Views: 1031

Answers (1)

yeuem1vannam
yeuem1vannam

Reputation: 957

When you want to start a new http session, let's use the base URL only, and send request to plain URL

uri = URI('https://fs.example.com:8443/services/trust/13/usermixed')
http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Post.new(uri.to_s)
request['Content-Type'] = 'application/soap+xml; charset=UTF-8'
request.body = @body(as xml)
response = http.request(request)

Then the request should work as expected.

Upvotes: 2

Related Questions