Sending http post request in Ruby by Net::HTTP
The second argument of Net::HTTP#post
needs to be a String
containing the data to post (often form data), the headers would be in the optional third argument.
POST request to HTTPS using Net::HTTP
Your request
hash is being replaced by your request
object which you're assigning Net::HTTP
. Also be sure to set request params in the body of your HTTP request:
require "active_support/all"
require "net/http"
token = "my_token"
url = "https://graph.facebook.com/v2.6/me/messages?"
sender = 100688998246663
text = "Hello"
request_params = {
recipient: {id: sender},
message: {text: text},
access_token: token
}
request_header = { 'Content-Type': 'application/json' }
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path, request_header)
request.body = request_params.to_json
http.request(request)
response = http.request(request)
You may find the following reference helpful: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
Sending a Post request with net/http
I don't know what your problem is but what about something like this
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'})
request.body = data.to_json
response = http.request(request)
ruby http request post method
Okay now that you have posted some information I am going to assume this is a json API because the responses are json.
Update Try this instead
require 'uri'
require 'net/http'
uri = URI.parse('https://ams.iaau.edu.kg/api/authentication/id/password')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
response = http.send_request('POST',uri.request_uri)
I am now getting a 401 Unauthorized as expected since I don't have an id or password
If you would prefer your original code I have found the solution which is setting the content type of the request directly e.g.
require 'uri'
require 'net/http'
url = URI.parse('https://ams.iaau.edu.kg/api/authentication/id/password')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
request = Net::HTTP::Post.new(url.path)
request.content_type = 'application/json' # HERE
response = http.request(request)
Trying to make HTTP Post request with a body using Ruby HTTP start block
Use the set_form_data
method to set your POST body:
Here is an example from the docs:
request = Net::HTTP::Post.new(uri)
request.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')
Ruby: Can net/http make a GET and POST request simultaneously?
When creating the request you just need to make sure to keep the GET params in the path:
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", {
'Referer' => "http://www.example.com/referer",
'User-Agent'=> "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
'Cookie' => $cookie
})
Notice that instead of just uri.path
, I append the ?
and uri.query
to it. This should pass the GET parameters as well as the POST ones.
Related Topics
Is There a Definitive Reference Document for Ruby Syntax
Retrieve All Posts Where the Given User Has Commented, Ruby on Rails
How to Return a Fixed Length Binary Representation of an Integer in Ruby
How to Find Out the Starting Point for Each Match in Ruby
Download and Write .Tar.Gz Files Without Corruption
In Depth Ruby Gem Development Resources (Book, Video, Sites)
How to Make Block Local Variables the Default in Ruby 1.9
Setting the Environment in Gemfile for Bundling Install/Update Based on a Customize File
Which Http Web Server How to Use to Debug Ruby Code Using Rubymine 3.0.1
Sorting an Array of Arrays in Ruby
Why Are My Thread Variables Intermittent in Rails
Ways to Define a Global Method in Ruby
Hash Remove All Except Specific Keys
Postgresql Ilike with Multiple Matches in Rails Activerecord
Rounding a Float to the Nearest Integer in Ruby