From: Engine Yard Date: 2009-07-30T14:50:03+09:00 Subject: Re: Posting an XML document to a protected API The parameters in Net::HTTP::start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) {|+http+| ...} are: address = SERVER_ADDRESS PORT = SERVER_PORT P_ADDR = PROXY_SERVER_ADDRESS P_PORT = PROXY_SERVER_PORT P_USER = PROXY_USER P_PASS = PROXY_USER_PASSWORD I am not really sure about what a proxy server and a proxy user are, but here is what happened in my case. The URL that my backend expected was of the format: http://1234@21.222.122.12:80/selection/books/andy-grove.xml?session_key=32klfd3n where '1234' was the API_KEY that was used to authenticate the calling application. If you look at it from a more general perspective this is similar to http:user_name:password@DOMAIN_NAME/PATH_TO_RESOURCE/RESPONSE_FORMAT?QUERY_PARAMS Now I just had to substitute the user_name field with the API_KEY which I was unable to do using: Net::HTTP::start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) {|+http+| ...} since the user and password here refer to the proxy user and the proxy password. But I found a simpler solution to this problem: 1. Set the 'Authorization' request header to 'Basic' and specify the base64 encoded 'API_KEY + :' as the username value and leave the password field empty. This will basically form a url of the form: http://encode64(API_KEY +':')@SERVER_IP_ADDRESS/PATH_TO_RESOURCE/BLAH_BLAH_BLAH To set the authorization header you can use: a) Net::HTTP.Post.basic_auth(username, password) b) Net::HTTP.Post.add_field('Authorization', 'Basic username:password) (NOT EXACTLY SURE IF THE SYNTAX IS RIGHT) Here is the link to ruby's documentation: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html pharrington wrote: > On Jul 27, 8:17�pm, Maruthy Mentireddi > wrote: >> ******************************************************************** >> HERE" >> 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, >> -- >> Posted viahttp://www.ruby-forum.com/. > > Since there doesn't seem to be alot of ri documentation about > Net::HTTP::Post and I'm too lazy to look at the source, > Net::HTTP.start looks like its invoked like this: > > ------------------------------------------------------- > Net::HTTP::start > Net::HTTP::start(address, port = nil, p_addr = nil, p_port = nil, > p_user = nil, p_pass = nil) {|+http+| ...} > ------------------------------------------------------------------------ > creates a new Net::HTTP object and opens its TCP connection and > HTTP session. If the optional block is given, the newly created > Net::HTTP object is passed to it and closed when the block > finishes. In this case, the return value of this method is the > return value of the block. If no block is given, the return value > of this method is the newly created Net::HTTP object itself, and > the caller is responsible for closing it upon completion. > > > so instead of loading the apikey and port and all into a single > string, have you tried using the parameters to the Net::HTTP.start > method as such? > > Eitherway, you might just be better of using curb for anything HTTP > related. -- Posted via http://www.ruby-forum.com/.