Created
August 25, 2010 02:32
-
-
Save dob/548731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you post to a Ruby on Rails REST API endpoint, then you'll get an | |
# InvalidAuthenticityToken exception unless you set a different | |
# content type in the request headers, since any post from a form must | |
# contain an authenticity token. | |
# | |
# This example shows you how to post to a rails endpoint. | |
require 'json' | |
def post_to_endpoint(endpoint) | |
uri = URI.parse(endpoint) | |
post_params = { | |
:title => "2BR Apartment For Rent in NYC", | |
:description => "Great midtown west location. I love this place.", | |
:price => "1500", | |
:api_key => "my_api_key" | |
} | |
# Convert the parameters into JSON and set the content type as application/json | |
req = Net::HTTP::Post.new(uri.path) | |
req.body = JSON.generate(post_params) | |
req["Content-Type"] = "application/json" | |
http = Net::HTTP.new(uri.host, uri.port) | |
response = http.start {|htt| htt.request(req)} | |
end |
you can also manually put the authentication tokens in the form submission request headers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
haaaaa, i use it to login redmine, to get login session cookie,