M
Maruthy Mentireddi
I am working on the FrontEnd of a website and need to make a RESTful
HTTP POST to a Backend system which checks for the presence of an API
KEY and a SESSSION KEY in the request URL as part of the security model.
Eg: To make a HTTP POST request, the URL should be of the format:
http://#{API_KEY}@#{BACKEND_HOST}:#{BACKEND_PORT}/#{PATH_TO_RESOURCE}?session_key=#{SESSION_KEY}
Now the problem I am facing is how do I specify the API_KEY in the URL
using the following code:
********************************************************************
server_addr = "http://#{API_KEY}@#{BACKEND_HOST_AND_PORT}"
api_path =
"#{BACKEND_HOST_PATH}/users/#{session[:user_id]}/contact_groups.xml?session_key=#{session[:session_key]}"
url = URI.parse(server_addr + api_path)
request =
Net::HTTP:ost.new(url.path+"?session_key=#{session[:session_key]}")
request.body = "<?xml version='1.0'
encoding='UTF-8'><contact_groups>SOME NESTED DATA GOES
HERE</contact_groups>"
response = Net::HTTP.start(url.host, url.port) {|http|
http.request(request)}
********************************************************************
When using the exact same code as above, the Backend responds back with
a HTTP 401 - !ruby/object:Net::HTTPUnauthorized error code.
And if I change the last line to include the API KEY as shown below:
response = Net::HTTP.start(url.user + "@" + url.host, url.port) {|http|
http.request(request)}
Ruby gives a"getaddrinfo: nodename nor servname provided, or not known"
error.
What is the right way to make this post to the Backend. Any response in
this regard will be greatly appreciated.
Thanks,
HTTP POST to a Backend system which checks for the presence of an API
KEY and a SESSSION KEY in the request URL as part of the security model.
Eg: To make a HTTP POST request, the URL should be of the format:
http://#{API_KEY}@#{BACKEND_HOST}:#{BACKEND_PORT}/#{PATH_TO_RESOURCE}?session_key=#{SESSION_KEY}
Now the problem I am facing is how do I specify the API_KEY in the URL
using the following code:
********************************************************************
server_addr = "http://#{API_KEY}@#{BACKEND_HOST_AND_PORT}"
api_path =
"#{BACKEND_HOST_PATH}/users/#{session[:user_id]}/contact_groups.xml?session_key=#{session[:session_key]}"
url = URI.parse(server_addr + api_path)
request =
Net::HTTP:ost.new(url.path+"?session_key=#{session[:session_key]}")
request.body = "<?xml version='1.0'
encoding='UTF-8'><contact_groups>SOME NESTED DATA GOES
HERE</contact_groups>"
response = Net::HTTP.start(url.host, url.port) {|http|
http.request(request)}
********************************************************************
When using the exact same code as above, the Backend responds back with
a HTTP 401 - !ruby/object:Net::HTTPUnauthorized error code.
And if I change the last line to include the API KEY as shown below:
response = Net::HTTP.start(url.user + "@" + url.host, url.port) {|http|
http.request(request)}
Ruby gives a"getaddrinfo: nodename nor servname provided, or not known"
error.
What is the right way to make this post to the Backend. Any response in
this regard will be greatly appreciated.
Thanks,