So I was using the splunk rest api to generate the search id thorugh post and then get the search results through a get request.
I am using httpparty, & this is my post and get request as follows:
def sid
self.class.post(
"/services/search/jobs",
headers: {'Content-Type' => 'application/json'},
basic_auth: #auth
).parsed_response
end
The generated sid looks like this:
1503402217.608
I make the necessary get request as follows:
def results(sid)
self.class.get(
"/services/search/jobs/#{sid}/results/",
headers: {'Content-Type' => 'application/json', 'Accept' => 'text/html'},
basic_auth: #auth
)
end
The above request gives a 204 no content.
However if I manually paste the sid in my code and run it, i get results back.
for eg: "/services/search/jobs/1503402217.608/results/"
So my question is how can interpolate the above to get results back.
EDIT: my dyamic get request looks this in the console:
<- "GET /services/search/jobs/1503402217.608/results/ HTTP/1.1\r\nContent-Type: application/json\r\nAccept: text/html\r\nAuthorization: Basic something==\r\nConnection: close\r\nHost: server:8089\r\n\r\n"
-> "HTTP/1.1 204 No Content\r\n"
-> "Date: Tue, 22 Aug 2017 11:43:37 GMT\r\n"
-> "Expires: Thu, 26 Oct 1978 00:00:00 GMT\r\n"
-> "Cache-Control: no-store, no-cache, must-revalidate, max-age=0\r\n"
-> "Content-Length: 0\r\n"
-> "Vary: Cookie, Authorization\r\n"
-> "Connection: Close\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Server: Splunkd\r\n"
-> "\r\n"
EDIT: Just so you know, this how i am parsing the sid in the first place before sending it as an argument to the GET request:
sid.parsed_response["sid"]
Related
I've read other answers on this topic, such as:
Parsing HTTParty response
HTTParty parsing JSON in Rails
However, I still can't figure out how to parse a response I'm receiving.
response.parsed_response = HTTParty.get(url, query: params) returns:
=> #<HTTParty::Response:0x89435d0 parsed_response="http://foo.com", #response=#<Net::HTTPOK 200 OK readbody=true>, #headers={"cache-control"=>["no-cache", "no-store"], "date"=>["Tue, 21 Feb 2017 23:10:47 GMT"], "expires"=>["Thu, 01 Jan 1970 00:00:00 GMT"], "p3p"=>["CP=\"ALL IND DSP COR CUR ADM TAIo PSDo OUR COM INT NAV PUR STA UNI\""], "pragma"=>["no-cache"], "server"=>["Apache-Coyote/1.1"], "set-cookie"=>["bar.Agent.p=c1921b97d1f8a0918621c48bd32ded2b; Domain=.bar.com; Expires=Fri, 19-Feb-2027 23:10:47 GMT; Path=/"], "content-length"=>["366"], "connection"=>["Close"]}>
I need the URL that appears after parsed_response. The other answers seemed to break down hashes that appear after parsed_response, but I'm just looking for the url that appears after parsed_response (and it only appears there in the response).
I tried:
puts response which returns the entire response above.
puts response.parsed_response which returns:
http://foo.com
=> nil
This usually works for me
response = HTTParty.get(url, options)
puts response.body
i was under the assumption that create_session if the endpoint.ex was configured to use the cookie store, would set the SET-COOKIE response header
# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
plug Plug.Session,
log: :debug,
store: :cookie,
key: "some_key",
signing_salt: "some_salt"
this is my authentication controller ( just a part of it)
def callback(%{ assigns: %{ ueberauth_auth: auth } } = conn, params) do
params = build_params(auth)
user = find_or_create_user params
conn = put_session(conn, :current_user, user)
IO.inspect conn.resp_headers
IO.inspect get_session(conn, :current_user)
render conn, "index.html"
#Helpers.redirect!(conn, "/")
end
def build_params(auth) do
%{email: auth.info.email, github_token: auth.credentials.token, github_user: auth.info.nickname}
end
def find_or_create_user(params) do
case DBRepo.get_by(User, email: params.email) do
nil ->
User.changeset(%User{}, params)
|> DBRepo.insert
results ->
results
end
end
IO.inspect conn.resp_headers
returns
[{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "vh8l2deodne1k2iloa4c3e4qdpmh857n"}, {"x-frame-options", "SAMEORIGIN"}, {"x-xss-protection", "1; mode=block"}, {"x-content-type-options", "nosniff"}]
IO.inspect get_session(conn, :current_user)
returns the user as expected
You don't see the session cookie in resp_headers because Plug.Session sets that cookie just before the response is actually sent, using Plug.Conn.register_before_send. If you make a request using any HTTP client (browser, curl, etc), you'll see the Set-Cookie header.
defmodule MyApp.PageController do
use MyApp.Web, :controller
def index(conn, _params) do
conn
|> put_session(:foo, :bar)
|> text("")
end
end
$ curl -I localhost:4000
HTTP/1.1 200 OK
server: Cowboy
date: Mon, 20 Feb 2017 08:57:36 GMT
content-length: 0
set-cookie: _my_app_key=SFMyNTY.g3QAAAABbQAAAANmb29kAANiYXI.F0G6lsgPxsYjq97tonLy1gRkOBUVcfwqKZdozgGRG-c; path=/; HttpOnly
content-type: text/plain; charset=utf-8
cache-control: max-age=0, private, must-revalidate
x-request-id: uoplksup9ndakf5sdr5shpjsjhvu849v
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
I'm trying to use Orange SMS API, I read how to use it here.
I want to use this api from Rails application.
I want to convert this curl command to httparty code.
curl -X POST \
-H "Authorization: {authorization_header}" \
-d "grant_type=client_credentials" \
https://api.orange.com/oauth/v2/token
I searched for many solutions, but all failed :(
here's the last try
HTTParty.post(
"https://api.orange.com/oauth/v2/token",
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
"Authorization" => "Basic {authorization_header}",
"Accept" => "application/json"
},
body: {
:grant_type => "client_credentials"
}.to_json
)
but I receive this error:
504 Gateway Time-out\nThe server didn't respond in time.\n\n", #response=#, #headers={"date"=>["Wed, 21 Sep 2016 13:35:37 GMT"], "cache-control"=>["no-cache"], "content-type"=>["text/html; charset=UTF-8"], "connection"=>["close"], "transfer-encoding"=>["chunked"]}>
irb(main):073:0>
I also tried this way
access_token = HTTParty.post(
"https://api.orange.com/oauth/v2/token",
headers: { "Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "Basic {authorization_header}"},
query: { "grant_type" => "client_credentials" }
)
but I get this error:
=> #"invalid_grant", "error_description"=>"The paramater grant_type is not valid."}, #response=#, #headers={"date"=>["Wed, 21 Sep 2016 13:14:18 GMT"], "host"=>["ope-apigee-private"], "content-type"=>["application/json"], "authorization"=>["Basic cWVnaUJlR0xhcndaYzZ0R1dtS1JqUFE2d0FBOVNEWW46SFJ1SGFURUJlOHR2aHlTOA=="], "x-forwarded-for"=>["213.181.226.205, 192.168.70.3", "192.168.9.45"], "x-forwarded-host"=>["api.orange.com"], "x-forwarded-server"=>["OPOPECBALLAP04"], "content-length"=>["86"], "connection"=>["close"]}>
I don't know how to use it I read the official way and it worked using Curl but I can't do it in Rails.
I am trying to convert the following curl command to ruby using net/http but I haven't figured out how to pass in the --data-urlencode script#files/jql/events.js part of the command.
curl https://mixpanel.com/api/2.0/jql -u <apikey>: --data-urlencode script#files/jql/events.js
Using net/http I had the following...
uri = URI.parse("https://mixpanel.com/api/2.0/jql")
request = Net::HTTP::Get.new(uri)
request.basic_auth("<apikey>", "")
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request(request)
end
Is there anyway to do this? If not within net/http then maybe using another gem?
Mixpanel has it's official ruby gem
I didn't actually work with it, but I assume it have all needed methods.
But if you don't like to use it, you may use Faraday an awesome HTTP client library for Ruby.
I made a simple example with it. Please have a look:
class MixpanelClient
def initialize(url = "https://mixpanel.com/api/2.0/jql", api_key = "ce08d087255d5ceec741819a57174ce5")
#url = url
#api_key = api_key
end
def query_data
File.read("#{Rails.root}/lib/qry.js")
end
def query_params
'{"from_date": "2016-01-01", "to_date": "2016-01-07"}'
end
def get_events
resp = Faraday.new(url: #url, ssl: { verify: false }) do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
faraday.basic_auth(#api_key, "")
end.get do |req|
req.params['script'] = query_data
req.params['params'] = query_params
end
raise MixpanelError.new("Mixpanel error") unless resp.status == 200
JSON.parse(resp.body)
end
end
class MixpanelError < StandardError; end
Here is the result:
[1] pry(main)> m = MixpanelClient.new
=> #<MixpanelClient:0x007fc1442d53b8 #api_key="ce08d087255d5ceec741819a57174ce5", #url="https://mixpanel.com/api/2.0/jql">
[2] pry(main)> m.get_events
I, [2016-06-09T09:05:51.741825 #36920] INFO -- : get https://mixpanel.com/api/2.0/jql?params=%7B%22from_date%22%3A+%222016-01-01%22%2C+%22to_date%22%3A+%222016-01-07%22%7D&script=function+main%28%29%7B+return+Events%28params%29.groupBy%28%5B%22name%22%5D%2C+mixpanel.reducer.count%28%29%29+%7D
D, [2016-06-09T09:05:51.741912 #36920] DEBUG -- request: Authorization: "Basic Y2UwOGQwODcyNTVkNWNlZWM3NDE4MTlhNTcxNzRjZTU6"
User-Agent: "Faraday v0.9.2"
I, [2016-06-09T09:05:52.773172 #36920] INFO -- Status: 200
D, [2016-06-09T09:05:52.773245 #36920] DEBUG -- response: server: "nginx/1.9.12"
date: "Thu, 09 Jun 2016 03:05:52 GMT"
content-type: "application/json"
transfer-encoding: "chunked"
connection: "close"
vary: "Accept-Encoding"
cache-control: "no-cache, no-store"
access-control-allow-methods: "GET, POST, OPTIONS"
access-control-allow-headers: "X-PINGOTHER,Content-Type,MaxDataServiceVersion,DataServiceVersion,Authorization,X-Requested-With,If-Modified-Since"
=> [{"key"=>["Change Plan"], "value"=>186}, {"key"=>["View Blog"], "value"=>278}, {"key"=>["View Landing Page"], "value"=>1088}, {"key"=>["login"], "value"=>1241}, {"key"=>["purchase"], "value"=>359}, {"key"=>["signup"], "value"=>116}]
A set ssl: {verufy: false} because Faraday need addtitional workaround to work with ssl certificates: https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates
I'm attempting to get Rails to play nice with the Digg API's OAuth. I'm using the oauth gem (ruby one, not the rails one).
My code looks approximately like this:
#consumer = OAuth::Consumer.new(API_KEY, API_SECRET,
:scheme => :header,
:http_method => :post,
:oauth_callback => "http://locahost:3000",
:request_token_url => 'http://services.digg.com/1.0/endpoint?method=oauth.getRequestToken',
:access_token_url => 'http://services.digg.com/1.0/endpoint?method=oauth.getAccessToken',
:authorize_url => 'http://digg.com/oauth/authorize')
#request_token = DiggController.consumer.get_request_token({
:oauth_callback => "http://xx.xxx.xxx.x:3000/digg/callback"
}, {
'Content-Type' => 'application/x-www-form-urlencoded'
})
session[:request_token] = #request_token.token
session[:request_token_secret] = #request_token.secret
redirect_to #request_token.authorize_url
Which is by-the-book in terms of what the gem documentation gave me. However, Digg spits a "400 Bad Request" error back at me when #consumer.get_request_token is called. I can't figure out what I'm doing wrong. Any ideas?
Edit: Code updated and Wireshark output added. My error is now "401 Authorization Required".
Output from Wireshark:
POST /1.0/endpoint?method=oauth.getRequestToken HTTP/1.1
Accept: */*
Connection: close
User-Agent: OAuth gem v0.3.6
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth oauth_nonce="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
oauth_callback="http%3A%2F%2Fxx.xxx.xxx.x%3A3000%2Fdigg%2Fcallback",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1268687137",
oauth_consumer_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
oauth_signature="xxx%2Bxxxxxxxxxxxxxxx%2Fxxxxxxx%3D", oauth_version="1.0"
Content-Length: 48
Host: services.digg.com
Content-Type=application%2fx-www-form-urlencoded
HTTP/1.1 401 Authorization Required
Date: Mon, 15 Mar 2010 21:05:37 GMT
Server: Apache
X-Powered-By: PHP/5.2.9-digg8
Cache-control: private
X-RateLimit-Current: 1
X-RateLimit-Max: 1000
X-RateLimit-Reset: 3600
X-Digg-Api-Version: 1.0
Accept-Ranges: bytes
Content-Length: 111
Keep-Alive: timeout=5, max=9998
Connection: Keep-Alive
Content-Type: text/xml;charset=utf-8
<?xml version="1.0" encoding="UTF-8"?>
<error code="5001" message="Invalid
signature" timestamp="1268687137"/>
Incidentally, the callback parameter should not be localhost:3000 but rather your public IP address (making sure to also open up port 3000 for external connections in your computer and/or router firewall(s)), or be left to the default (out-of-band.)
Examine the contents of the OAuth::Unauthorized exception which gets thrown (or use a sniffer such as tcpdump or Wireshark) to get additional details about the HTTP 400 error (they are probably having issues with some of your parameters.)