How to Make Ruby's Restclient Gem Respect Content_Type on Post

How do I make Ruby's RestClient gem respect content_type on post?

You might want to put json as string as your payload instead of hash. For example, do:

RestClient.post 'http://localhost:5001','{"a":"b"}',:content_type => 'application/json'

If you look at the payload.rb, it shows that it will use the Base clase instead of UrlEncoded class if the payload is string. Try that and see if that work for you.

JSON Rest-Client Authorization in Rails

I noticed my content_type was not being updated, searched it up and StackOverflow came through for me.
How do I make Ruby's RestClient gem respect content_type on post?

I basically just changed my request to be:

RestClient.post 'http://localhost/WebService/AuthenticateLogin', '{"RuntimeEnvironment":1, "Email":"someone@example.com", "Password":"Pa$$worD"}', :content_type => "json"

I merely added quotes around my params like so: '{params}'
And instead of using => in the params, I changed to colons just like the actual request I used in cURL and everything worked like a charm.

Also note I quoted the "json" content_type.

Thanks @rlarcombe for your help.

How can I get Ruby's RestClient to use a multi-valued query parameter?

You can pass the parameters in as a string:

resource.get(params: 'p=1&p=2')

For instance, using the restclient shell:

>> RestClient.log = Logger.new(STDOUT)
#<Logger:0x007fa444cbecc0 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fa444cbec70 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007fa444cbebd0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fa444cbeba8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fa444cbeb58>>>>
>> resource = RestClient::Resource.new( 'http://www.example.net' )
#<RestClient::Resource:0x007fa444c9fdc0 @url="http://www.example.net", @block=nil, @options={}>
>> resource.get(params: 'p=1&p=2')
RestClient.get "http://www.example.net", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Params"=>"p=1&p=2"
# => 200 OK | text/html 1270 bytes
"<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"

If you don't want to write code to build a string, which you should avoid because it's not necessarily straightforward, let Ruby's URI class cobble it together:

require 'uri'
URI::encode_www_form([['p', 1], ['p', 2]])
# => "p=1&p=2"

Setting Request Headers in Ruby

The third parameter is the headers hash.

You can do what you want by:

response = RestClient.post( 
url,
request,
:content_type => :json, :accept => :json, :'x-auth-key' => "mykey")

Calling Elasticsearch Rest API from Ruby script

I just started using elasticsearch ruby client and its solved the purpose and its better to use elasticsearch ruby client to do operations in elasticsearch because ruby client provides connection pooling (connecting through multiple elasticsearch nodes)

http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch



Related Topics



Leave a reply



Submit