Why mechanize failed to login website ruby - ruby

I'm trying to make an account to website "vpnstaticip.com" using mechanize
but I've always got unknown error that
ERROR: Email is too long
after submiting.
The error never happend in the browser
I've tried to submit without any input and same thing happend
require "mechanize"
$url = "https://vpnstaticip.com/create-account.php?trial=1"
$m = Mechanize.new
$page1 = $m.get($url)
$form1 = $page1.form_with(:id => "pro_form1")
$form1.field_with(:name => "name").value = "name"
$form1.field_with(:name => "email").value = "me#mail.com"
$form1.field_with(:name => "country").options[217].click #United States
$form1.field_with(:name => "username").value = "Username"
$form1.checkbox_with(:name => "terms").check
$page2 = $m.submit($form1)
$file1 = open("vpnstaticip.html","w")
$file1.write($page2.parser)
$file1.close()

Looks like this form submitted by js, but the mechanize is not working with js actually
onclick="document.getElementById('pro_form1').submit();"
perhaps the changed of driver will help (selenium or poltergeist) with the creation of Capybara browser session.
or, as an option, just try to ignore SSL errors:
$m.verify_mode = OpenSSL::SSL::VERIFY_NONE
or i think, match better to use here the net-post request without mechanize, for me it's work fine:
url = URI("https://vpnstaticip.com/create-account.php?trial=1")
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)
request["content-type"] = 'application/x-www-form-urlencoded'
request.body = "name=name&email=me%2540mail.com&country=217&username=Username&terms=1&nospam=nospam&submitted=1"
response = http.request(request)
puts response.read_body
=> ERROR: Username already exists. Try different one
besides, i can't reproduce the "Email is too long" error, so please, let me know if the problem is still persist, thanks

Related

Got the error File type is not supported when uploading a file in ruby on rails

url = URI("https://api.podium.com/v4/messages/attachment")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = "multipart/form-data"
request["Authorization"] = "Bearer #{access_token}"
form_data = [["attachment",File.open('D:\proj\v5\ap\fl\Screenshot (1).png')],['data', "#{request_data}"]]
request.set_form(form_data, 'multipart/form-data')
response = https.request(request)
response_body = JSON.parse(response.body)
if response.code == '200' || response.code == '201'
return response_body,'success'
else
return response_body,"#{response.message}"
end
rescue Exception => ex
return ex,'Exception'
end
**
When i am sending the request i got the error like
{"code"=>"invalid_request_values", "message"=>"File type is not supported.", "moreInfo"=>"https://docs.podium.com/docs/errors#invalid_request_values"}
**
Here are a couple of things you could try:
The podium documentation says that the images cannot be above 5mb in size. You can verify if this is the case.
https://help.podium.com/hc/en-us/articles/360039896873-Sending-Messages#Attach%20media%20to%20a%20message
I noticed the code snippet you've shared does set have this line as mentioned in their documentation here https://docs.podium.com/reference/messagesend_with_attachment
request["accept"] = 'application/json'
Maybe adding this header might fix it for you, as you are saying that it is working for you in Postman but not in Ruby.
Try uploading the file from the API Doc reference page itself and check out the code sample they provide there. There are some differences in the code sample you've shared, and the one that podium shows in their doc.

How to add "Content-type" header in HTTP POST

I keep getting a
400 "Bad Request" (Net::HTTPServerException)
error whenever I try to add a content-type header from various methods.
I've seen several different examples and I can't get any to work. My goal is to add a content type of JSON to my request. Without the header, my request doesn't error:
def post_data(notice)
uri = URI('my uri is here')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
text = notice
req.add_field('Content-Type', 'application/json')
req.body = "{\"sensu_payload\" = #{payload(text).to_json}}"
response = http.request(req)
verify_response(response)
end
I've also tried this method of adding a header:
request = Net::HTTP::Post.new(#path, initheader = {'Content-Type' =>'application/json'})
Use uri.path instead of #path:
request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
Instead of add_field, I think you should use the hash [] form:
req['Content-Type'] = 'application/json'
See the "Setting Headers" example in the documentation and []=.
Also, using:
"#{uri.path}?#{uri.query}"
is never a good idea. String concatenation can't manage the complexities of correctly encoding illegal values in a query, which can break a request. Consider doing something like this instead:
require 'uri'
require 'uri'
foo = URI('http://www.example.com') # => #<URI::HTTP http://www.example.com>
foo.query = URI::encode_www_form({'bar' => 'path/to/file', 'baz' => 'this & that'})
foo.to_s # => "http://www.example.com?bar=path%2Fto%2Ffile&baz=this+%26+that"
Beyond that, I'd recommend using any of the other HTTP-client gems available. They make it much easier to deal with unexpected situations, like redirects and retries than Net::HTTP. It's more like the building block for features that aren't available other ways.

Ruby: Syntax to POST batch request with Net::HTTP to Facebook API

Using curl the following command is correct, and the Facebook API replies with the expected response:
curl -F 'access_token=TOKEN' -F 'batch=[{"method":"GET",
"relative_url":"facebook"},{"method":"GET",
"relative_url":"youtube"}]' https://graph.facebook.com
I would like to convert this to Ruby but have been struggling to find the right syntax. I have tried variations on the below, but without luck:
uri = URI.parse('https://graph.facebook.com')
res = Net::HTTP.post_form(uri, 'access_token' => 'TOKEN', 'batch' =>
"[{'method':'GET', 'relative_url':'facebook'}]")
Can you help?
Thanks to this question and this blog post here's what I got working:
uri = URI("https://graph.facebook.com/")
req = Net::HTTP::Post.new(uri.path)
attach = {}
attach = {'batch' => [{"method" => "GET", "relative_url"=>"facebook"}].to_json}
req.set_form_data(attach.merge('access_token' => "TOKEN"))
res = Net::HTTP.new(uri.host, uri.port)
res.verify_mode = OpenSSL::SSL::VERIFY_NONE
res.use_ssl = true
response = nil
res.start do |http|
response = http.request(req)
end

ruby net/http difficulties

I have some perl code that I'm trying to port to ruby. The perl code does what I want to, but I'm having some difficulty getting similar results out of the ruby code which is all the more frustrating because what I'm doing isn't terribly complicated.
first, the perl code:
use LWP::UserAgent;
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new(file => "/home/blah/lwpcookies.txt", autosave => 0);
my $ua = LWP::UserAgent->new('cookie_jar' => $cookie_jar);
my $p = {
'param1' => 'p1val',
'param2' => 'p2val',
'param3' => 'p3val',
'param4' => 'p4val',
'param5' => 'p5val',
'param6' => 'p6val',
};
my $res = $ua->post('https://sitename.somesite.com/login_page.php', $p); #login
my $url = "https://sitename.sometime.com/report.php?startdate=2012-1-1&enddate=2012-1-2";
$res = $ua->get($url);
I can then access $res->content and get what I want out of it.
I've tried the same in ruby using net/http, but I'm not able to get the same results. I'm also having some trouble figuring out what parts are even not working.
Here's the ruby code:
require 'net/http'
params = Hash.new
params['param1'] = 'p1val'
params['param2'] = 'p2val'
params['param3'] = 'p3val'
params['param4'] = 'p4val'
params['param5'] = 'p5val'
params['param6'] = 'p6val'
uri = URI.parse('https://sitename.somesite.com/login_page.php')
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.request_uri)
request.set_form_data(params)
res = http.request(request)
cookies = res.response['set-cookie']
# for what it's worth, I'm pretty sure the problem has already occurred by this point
uri = URI.parse("https://sitename.somesite.com/report.php?startdate=2012-1-1&enddate=2012-1-2")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request['Cookie'] = cookies
res = http.request(request)
Thoughts? Suggestions? Tell me why I'm an idiot? Thanks.
Try Mechanize, it does cookies and redirects for you:
require 'mechanize'
agent = Mechanize.new
agent.post url1, params
cookie is set now
response = agent.get url2

curb post over ssl

how can you do a post over https using curb ruby gem?
This is how I do it over http to post a file to a server:
c = Curl::Easy.new("http://www.myserver.com/upload_messages")
c.multipart_form_post = true
post_field = Curl::PostField.content('fieldname', myfile)
c.http_post(post_field)
With net::http I would use use_ssl = true but how to do it with curb?
The post goes to an application running on heroku and the error I get now is:
Curl::Err::SSLCaertBadFile (Curl::Err::SSLCaertBadFile)
Thanks.
Have you tried c = Curl::Easy.new("https://www.myserver.com/upload_messages") (note the https instead of http on the url), cause in this example (https://github.com/taf2/curb/blob/master/samples/gmail.rb) from Curb's Github (that I suppose works, haven't tried it myself) they post to Gmail through https just doing that.
It seems that you have bad certificates and there should be option to trust unsigned.
require 'net/http'
require 'net/https'
require 'uri'
url = URI.parse 'https://myname:mypass#mail.google.com/'
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Also try curb-fu gem. There you can make ssl post request like
form_data = { :color => 'red', :shape => 'sphere' }
response = CurbFu.post({:url => "https://example.com", :protocol => "https"}, form_data)

Resources