Following is my code, I am trying to post a form to a https site. I've googled many times with no luck.
post_params = {
name: "Peter",
age: "26",
school: "St. Andrew Secondary"
}
uri = URI.parse('https://www.somehttps.com')
http = Net::HTTP.new(uri, "443")
http.use_ssl = true
http.post_form(uri, post_params)
But I always got:
NoMethodError (undefined method `post_form' for #<Net::HTTP www.somehttps.com:443 open=false>)
Why is it so? Please help me, thank you.
Try - Net::HTTP.post_form(uri, post_params)
Related
As an example, this scenario will fail with an error, "cannot convert, not a json string: [type: NULL, value: null]":
#report=false
Scenario: POST request; 200 response
Given url 'http://localhost:8080'
And request { "id": "123" }
When method post
* def requestBody = karate.prevRequest.body
* json requestJson = requestBody
However, if you remove the #report=false tag (or set it to 'true'), the request body is captured just fine.
Is this a bug or am I missing something obvious here?
Thanks!
This has been resolved with v0.9.9
It may be a bug, but can you please use the 1.0 series: https://github.com/intuit/karate/wiki/1.0-upgrade-guide
All development is focusing on that, and as of now you should be able to test 0.9.9.RC4 - and if you still see an issue, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
I would like to know how to use rails as backend for my iOS app.
All I need is a User with email and password to authenticate using devise.
I already have a User created with devise and rails 4.
I did find this post http://jessewolgamott.com/blog/2012/01/19/the-one-with-a-json-api-login-using-devise/ explaining what I need, but some things are still missing.
When I try to do a POST via my iOS app, I get the message "Can't verify CSRF token authenticity". How do I solve that without skipping the filter verify_authenticity_token ?
How would the request code for the iOS look like? Right now I'm doing a POST to http://localhost:3000/api/users/sign_in.json and setting the HTTPBody = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&jsonError], but the rails server is receiving only a string as key with the entire json dictionary, not an actual json dictionary.
params = {"{\"user\":{\"email\":\"qwe\",\"password\":\"123\"}}"=>nil, "action"=>"create", "controller"=>"api/sessions", "format"=>"json"}
How would I do an https request instead of http, so I can hide the password and email fields in case someone else tries to watch my internet traffic?
Thank you very much.
To use Rails Applications Mobile and Android and IOS, necessarily you have to use JSONP: example:
JS sample:
$.ajax({
url: '/api_mobile',
jsonp: "callback",
dataType: "jsonp",
cache: true,
data: {method: 'login', other_data ...},
success: function(res) {
// response object
console.log(res)
},
error: function(request, status, error) {
alert("Error server: " + request.status);
}
});
RAILS 4:
protect_from_forgery with: :exception, only: :api_mobile
# route /api_mobile
def api_mobile
json = {error: 'Not found Method'}
case params[:method]
when: 'login'
if User.login(params[:username], params[:password])
json = {notice: 'Login success'}
else
json = {error: 'Error Username or Password'}
end
end
render json: json, :callback => params[:callback]
end
All functions must be personalized and parameterized
Im trying to implement rspec for a http call... But I have not succeeded so far..
http = Net::HTTP.new("secured site url", "443")
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.get("/query params",initheader = {'Accept' =>'application/xml'})
return response
I would need an rspec for this..Though Im new to this, I tried many ways but it says expected: 1 time received: 0 times
Here is all what I have written :
it "should be success" do
#mock_http = mock("http")
#mock_http.should_receive(:new).with("secured site url")
end
Please correct me if Im wrong.. Any help is appreciated! Thanks
Of course you have to activate the method in which the new statement is called so your code should look something like:
it "should be success" do
#mock_http = mock("http")
Net::HTTP.should_receive(:new).with("secured site url", "443")
some_method
end
def some_method
http = Net::HTTP.new("secured site url", "443")
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.get("/query params",initheader = {'Accept' =>'application/xml'})
return response
end
I'm having trouble with tastypie and posting data to it. I only am able to retrieve a 401 error code.
For clarification, I am able to successfully retrieve data from the tastypie api.
Attached are the code snippets, and maybe someone can help me out get behind this.
Before I get started, a little background: I am using a custom authorization class.
class CustomAuthorization(Authorization):
def is_authorized(self, request, object=None):
if request.user.username == 'custom_user':
return True
return False
Here is the actual resource:
class CustomObjectResource(ModelResource):
class Meta:
queryset = CustomObject.objects.all()
authentication = ApiKeyAuthentication()
authorization = CustomAuthorization()
list_allowed_methods = ['get', 'post', ]
detail_allowed_methods = ['get', 'post', 'put']
include_resource_uri = False
resource_name = 'customobject'
always_return_data = True
def obj_create(self, bundle, request=None, **kwargs):
try:
print "request"
except:
raise BadRequest('I couldnt save your information.')
return True
I know the obj_create method is bogus, but it should still be called and do something, or is this already the issue?
The following curl command is used to post the data to the tastypie API.
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my lst post.", "pub_date": "2011-05-22T00:46:38", "slug": "another-post", "title": "Another Post"}' http://local.com:8000/api/v1/customobject/?format=json&username=custom_user&api_key=123456789012345
The api_key is correct, but bogus in this case!
As previously mentioned, the get method works but the post just wont work.
Anyone have an idea on how to solve this or have a workaround?
I would try a couple of things to debug this issue.
1) Try adding: allowed_methods = ['get', 'post', 'put']
2) Add print statements in the custom_authorization to check if that is causing the problems due to the request.user.username being different.
3) Do (2) in the source of APIKeyAuthentication too.
This should be sufficient for you to debug the issue.
Remember to remove the print statements once youre done!
Best of luck.
This is COULD be due to a known issue. On the background tastypie at the moment converts the POST to PUT and as Nikunj pointed since in list_allowed_methods you don't have PUT the POST gets blocked too... Not sure there though cause you should get method not allowed in that case. I would suggest debug in the method "is_authorized" and check what is happening there.
In a rails 3 app, I'm using a third party API to save webcam videos. When a video is saved to their server, they give an option to send you a callback to a url you specify. From their site:
Callback
Callbacks are for advanced users who wish to be notified when a video is finished processing on the Framey servers. If you choose to receive callbacks in your account settings Framey will POST video and thumbnail URLs to your server once conversion is complete.
Here's an example of the data that we post to your server:
"video": {
"name": "ecf7c330-549c-012e-9d34-7c6d628c53d4",
"filesize": 123456,
"duration": 15.62,
"state": "uploaded",
"views": 0,
"data": {
"my_user_id": 1,
"video_title": "Contest Submission"
},
"flv_url": "http://framey.com/videos/source/ecf7c330-549c-012e-9d34-7c6d628c53d4.flv",
"mp4_url": "http://framey.com/videos/source/ecf7c330-549c-012e-9d34-7c6d628c53d4.mp4",
"large_thumbnail_url": "http://framey.com/thumbnails/large/ecf7c330-549c-012e-9d34-7c6d628c53d4.jpg",
"medium_thumbnail_url": "http://framey.com/thumbnails/medium/ecf7c330-549c-012e-9d34-7c6d628c53d4.jpg",
"small_thumbnail_url": "http://framey.com/thumbnails/small/ecf7c330-549c-012e-9d34-7c6d628c53d4.jpg"
}
I'm working my way to understanding how to handle this, but I thought I could get proper direction from you good folks. They specify a POST method, so I have to set up a route. In my config/routes.rb file I have
post "framey/callback/"
In my app/controllers/framey_controller.rb file there is this:
class FrameyController < ApplicationController
def callback
end
end
Do I need to do something like
render :json =>????
I'm familiar with using POST via HTML forms and/or ajax. I'm not sure how to grab the JSON they send back to me.
Thanks in advance for the help and please let me know if you need more details.
In app/controllers/framey_controller.rb
class FrameyController < ApplicationController
def callback
v = Video.new
v.name = params[:video][:name]
v.user_id = params[:video][:data][:user_id]
v.question_id = params[:video][:data][:question_id]
v.save
end
end