I'm able to POST to the Slack incoming API endpoint via CURL, but when trying with the below its not working. I'm assuming the formatting if off. How can I fix this?
parms = {text: text_for_slack, channel: "#customer_sessions", username: "SessionBot", icon_emoji: ":raised_hands:"}
x = Net::HTTP.post_form(URI.parse(ENV['SessionSlackURL'].to_s), parms.to_s)
You can post using two methods (text from slack configuration for incoming webhook) :
You have two options for sending data to the Webhook URL above:
Send a JSON string as the payload parameter in a POST request
Send a JSON string as the body of a POST request
json in the body.
require "net/http"
require "uri"
require "json"
parms = {
text: text_for_slack,
channel: "#customer_sessions",
username: "SessionBot",
icon_emoji: ":raised_hands:"
}
uri = URI.parse(ENV['SessionSlackURL'])
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.body = parms.to_json
response = http.request(request)
json as parameter
parms_form = {
"payload" => {
text: text_for_slack,
channel: "#customer_sessions",
username: "SessionBot",
icon_emoji:":raised_hands:"
}.to_json
}
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(parms_form)
Related
I want to reply a review from google app.
This is link document: https://developers.google.com/android-publisher/api-ref/reviews/reply
I use Net::HTTP to post data.
my method to reply review
def self.reply_review(package , access_token, review_id, text )
uri = URI("https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}")
puts "https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
params = {
"replyText" => text
}
request = Net::HTTP::Post.new(
uri.request_uri,
'Content-Type' => 'application/json'
)
request.body = params.to_json
response = http.request(request)
puts response
puts(response.body)
response
end
but it always response
=> => #<Net::HTTPBadRequest 400 Bad Request readbody=true>
I am sure my data into accurate (packageName, access_token, replyId).
And how to fix this to reply a review use Net::HTTP
I have used this to connect with google Verification API.
require 'openssl'
require 'net/http'
uri =URI.parse("https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri,
initheader = {'Content-Type' =>'application/json'})
request.form_data = {"replyText" => text}
response = http.request(request)
Trying to add header in the request
email = "blahblah#gmail.com"
token = "abcdefghijk"
url = "http://www.somewebsite.com/request?params1=value1¶ms2=value2"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port).start
request = Net::HTTP::Get.new(uri.request_uri)
request['email'] = email
request['token'] = token
response = http.request(request)
render json: response.body
The results i have gotten back is {"error":"invalid request parameters"}
I am supposed to get back a list of data in json. I tried using postman to test if the url is working and passed the email and token inside header and i got back the data that i wanted.
I am not sure where it went wrong with the code. Can anybody advise me which part did i do wrongly? Thanks a lot!
Try this.
email = "blahblah#gmail.com"
token = "abcdefghijk"
url = "http://www.somewebsite.com/request?params1=value1¶ms2=value2"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port).start
request = Net::HTTP::Get.new(uri.request_uri, {'email'=>email,'token'=>token})
response = http.request(request)
render json: response.body
OR
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("email", email)
request.add_field("token", token)
response = http.request(request)
render json: response.body
I am required to make an http request with a header similar to the one quoted below.
POST /approval HTTP/1.1
Content-Type:text/xml
Content-Length:569
Host:127.0.0.1
Authorization: WSSE realm="SDP", profile="UsernameToken"
X-WSSE: UsernameToken Username="xxxxxxxxxxxxxxx", PasswordDigest=" xxxxxxxxxxxxxxxx", Nonce=" xxxxxxxxxxxxxx", Created="2012-07-26T11:31:26Z"
X-RequestHeader: request ServiceId="xxxxxxxxxxxxx", TransId=" xxxxxxxxxxxxxxxxxxx" , LinkId="xxxxxxxxxx", FA="xxxxxxxxxx"
Cookie: sessionid=default8fcee064690b45faa9f8f6c7e21c5e5a
Msisdn: 861111111
X-HW-Extension: k1=v1;k2=v2
<ns2:preapprovalrequest xmlns:ns2="http://www.xxxxxxxx.com">
<fromfri>ID:2341305205559/MSISDN</fromfri>
<tofri>ID:2341305205559/MSISDN</tofri>
<message>abc</message>
</ns2:preapprovalrequest>
I have attempted to make the Net::HTTP Ruby 2.2.0 library with something similar to this.
url = 'http://xxx.xxx.xxx.xxx:xx/xxx/xxx/approval'
request_xml = "<?xml version='1.0' encoding='UTF-8'?><ns2:approvalrequest xmlns:ns2='http://www.xxxxxxxx.com'><fromfri></fromfri><tofri></tofri><message></message></ns2:approvalrequest>"
uri = URI(url)
req = Net::HTTP::Post.new(uri.path)
req.content_type = 'text/xml'
req['Authorization']['/']="WSSE"
req['Authorization']['realm']= "xxxxx"
req['Authorization']['profile']="xxxxxxxx"
req['X-WSSE']['/']="UsernameToken"
req['X-WSSE']['Username']=""
req['X-WSSE']['PasswordDigest']=""
req['X-WSSE']['Nonce']=""
req['X-WSSE']['Created']=""
req['X-RequestHeader']['/']="request"
req['X-RequestHeader']['ServiceId']=""
req['X-RequestHeader']['TransId']=""
req['X-RequestHeader']['LinkId']=""
req['X-RequestHeader']['FA']=""
req.body = request_xml
response = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
result = Hash.from_xml(response.body)
However, this throws errors **NoMethodError: undefined method[]=' for nil:NilClass.on the line**req['Authorization']['/']="WSSE"`.Any idea how to construct a proper header file with multiple fields.
you are operating on an empty hash, either merge into it or assign it properly
require 'net/http'
req = Net::HTTP::Post.new('bla')
req.content_type = 'text/xml'
req['Authorization'] = {
'/' => "WSSE",
'realm' => "xxxxx",
'profile' =>"xxxxxxxx",
}
puts req['Authorization'] # => {"/"=>"WSSE", "realm"=>"xxxxx", "profile"=>"xxxxxxxx"}
I am trying to make an http request to send and sms through kannel using ruby but its not working,what could be the problem with the code in this method. Thanx
require 'net/http'
def self.send_sms( to, from, message)
id = rand(36**8).to_s(36)
uri= URI('http://localhost:13013/cgi-bin/sendsms?')
params = {username: 'skylinesms', password: 'password', from: '#{from}',
text: '#{message}',
'dlr-url': '#{Rails.application.routes.url_helpers.deliver_url(:id => id)}',
'dlr-mask': 3
}
uri.query = URI.encode_www_form(params)
req = Net::HTTP::Get.new(uri.to_s)
res = Net::HTTP.get_response(uri)
res
end
I am trying to implement call this API method in Ruby https://zferral.com/api-docs/affiliate#aff-create-usage-example which expects this object:
{ "affiliate" : {
"email" : "john#example.com",
"send_user_email" : "1" }}
Using a Httparty class, I call this:
result = self.class.post("/api/#{#apikey}/affiliate/create.xml", :body => {:affiliate => {:email => email}})
Unfortunately, the API keeps sending me "Email is required". I have tried to switch to json, I am changed :body with :query, etc...
Anybody could show me how to call the affiliate/create method correctly?
Thank you.
You're sending json, but posting it to create.xml --- in rails, this will auto-set the content type to be xml when you post to create.xml
Try posting to /api/#{#apikey}/affiliate/create.json
If that doesn't work --- are you following the class design that HTTParty really likes? http://railstips.org/blog/archives/2008/07/29/it-s-an-httparty-and-everyone-is-invited/
This is how I fixed my issue:
url = URI.parse('https://xxxx.zferral.com/api/xxx/affiliate/create.xml')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url.path)
request.body = "<?xml version='1.0' encoding='UTF-8'?><affiliate><email>#{email}</email><send_user_email>0</send_user_email></affiliate>"
request.content_type = 'text/xml'
response = http.request(request)