accessing Name and value pair using "Sinatra"

J

Jesús Gabriel y Galán

Hello everyone,

=A0 I currently using Sinatra nad i must say its great.
=A0 But I am currently stuck i wanted to passes a Name,value pair
=A0 as a parameter to sinatra

=A0 something like this
=A0 http://localhost:4567/multiQuery?&Name_1=3D'ABC'&Name_2=3D'XYZ'&Name_= 3=3D'LMN'

=A0 so that i can directly access values 'ABC','XYZ' and 'LMN'

There's a params attribute in Sinatra::Base that contains the query string:

This route:

get '/hello/:name' do |name|
"Hello, #{name}. #{params.inspect}"
end

when called as:

http://localhost:4567/hello/x?a=3D3&b=3D4

produces:

Hello, x. {"name"=3D>"x", "a"=3D>"3", "b"=3D>"4"}

Hope this helps,

Jesus.
 
K

Ken Ghosh

Jesús Gabriel y Galán said:
There's a params attribute in Sinatra::Base that contains the query
string:

This route:

get '/hello/:name' do |name|
"Hello, #{name}. #{params.inspect}"
end

when called as:

http://localhost:4567/hello/x?a=3&b=4

produces:

Hello, x. {"name"=>"x", "a"=>"3", "b"=>"4"}

Hope this helps,

Jesus.

can i get only parameters after the question '?'
e.g
http://localhost:4567/hello/x?a=3&b=4
as
{"a" => "3","b" => "4"}
also i want to know how can i return and xml content

so that when i type
http://localhost:4567/hello/x?a=3&b=4
the response should be presented in xml format
 
K

Ken Ghosh

Michael said:

please help with these code in mind

####### this is my sinatra code############3

get '/multiquery.xml' do
content_type('text/xml')
array_of_names = params[:name]
'#{construct_xml(array_of_names)}'
end

the method construct_xml look like this

def construct_xml(names)
xml = Builder::XmlMarkup.new:)indent =>1)
xml.instruct!
names.each do |name|
xml.queryresponse {
xml.name "#{name}"
xml.credential "Engineer"
}
end
return xml
end

Now the point is that the method "construct_xml" construct the
appropriate xml
but the the xml doesn't appear in browser
and the line of code '#{construct_xml(array_of_name)}' run into "XML
Parsing Error: not well-formed"

iam sure that xml is formed properly.i mean to say the i have set the
:target => $stdout

like this
Builder::XmlMarkup.new:)target =>$stdout ,:indent =>1)
and the xml is constructed properly in console
ent .Is there any thing that I am missing.


please reply

thanks anyway
 
B

Brian Candler

Ken said:
####### this is my sinatra code############3

get '/multiquery.xml' do
content_type('text/xml')
array_of_names = params[:name]
'#{construct_xml(array_of_names)}'
end

Single quotes prevent any expansion taking place. In this case, you are
literally returning the string

#{construct_xml(array_of_names)}

to the browser.

Use double quotes, or better, just omit the "#{...}" construction
entirely, since you're just putting a string inside a string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top