Sending Http Post Request in Ruby by Net::Http

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



Leave a reply



Submit