如何在Rails中执行Get/Post/Put请求
require 'open-uri'
require 'json'
require 'net/http'
class CoupleController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:raw_data_put] def raw_date_get
#如果有GET请求参数直接写在URI地址中
begin
uri = 'http://qa.services.theknot.com/registry/v1/couples/2170045'
html_response = nil
open(uri) do |http|
html_response = http.read
end
data = JSON.parse(html_response)
registries=data["CoupleRegistries"]
render json: data.has_key?("Id")
rescue (excetpion)
render json: excetpion
end
end def raw_data_put
require 'net/http'
begin
retailerRegistry=Hash.new()
retailerRegistry["RetailerId"]=12160
retailerRegistry["RetailerRegistryCode"]="cjq2I5Wc3u_zIU_QFkvy0w"
retailerRegistry["RegistrantFirstName"]="Kelly"
retailerRegistry["RegistrantLastName"]="Bu"
retailerRegistry["RegistrantEmail"]="zzkelly.burch.890@gmail.com"
retailerRegistry["CoRegistrantFirstName"]="Zacuu"
retailerRegistry["CoRegistrantLastName"]="Wilson"
retailerRegistry["CoRegistrantEmail"]=""
retailerRegistry["EventDate"]="2014-11-15"
retailerRegistry["EventTypeId"]=1
retailerRegistry["IsDeleted"]=false
retailerRegistry["EventDescription"]=""
retailerRegistry["ReferralStatusCode"]=""
retailerRegistry["EventTypeId"]=1
retailerRegistry["City"]=""
retailerRegistry["State"]="GA"
retailerRegistry["Zip"]=""
retailerRegistry["Country"]=""
retailerRegistry["AltRetailerRegistryCode"]="TargetUCfnNSDSM"
#port = 80
host = 'qa.services.theknot.com'
path = '/registry/v1/raw/retailerRegistry' req = Net::HTTP::Put.new(path, initheader = {'Content-Type' => 'application/json'})
req.body = retailerRegistry.to_json
response = Net::HTTP.new(host).start { |http| http.request(req) }
puts response.code
render json: response.code
rescue Exception => e
puts e.message
puts e.backtrace.inspect
render json: e
#render json: '{"result":"failed"}'
end
end def raw_data_post
begin
affiliate=Hash.new()
affiliate["Id"]=2
affiliate["Name"]="Test0003"
affiliate["AffiliateCode"]="Test0003"
affiliate["AffiliateUrl"]="Test0003"
affiliate["Description"]="Test0003"
host = 'qa.services.theknot.com'
path = '/registry/v1/affiliates'
req = Net::HTTP::Post.new(path, initheader = {'Content-Type' => 'application/json'})
req.body = affiliate.to_json
response = Net::HTTP.new(host).start { |http| http.request(req) }
puts response.code
render json: response.body
rescue Exception => e
puts e.message
puts e.backtrace.inspect
render json: e.message
end
end
end
Routes:
get 'couple/raw_data_get'
  put 'couple/raw_data_put'
  get 'couple/raw_data_post'
如何在Rails中执行Get/Post/Put请求的更多相关文章
- Fiddler抓取Intellij Idea中执行的web网络请求
		
首先可以打开命令行 输入:ipconfig 找到本机配置的IP地址 这里是: 192.168.97.122 或者打开Fiddler 点击如下图片中的小三角符号:将鼠标放在online的位置,也可以看到 ...
 - 【转】如何在 Windows 中执行干净启动
		
完成故障排除后,请执行以下步骤将计算机重置为正常启动. Windows 8.1 和 Windows 8 从屏幕右边缘滑入,然后点按“搜索”.您也可以将鼠标指向屏幕的右下角,然后单击“搜索”. 在搜索框 ...
 - 如何在 Rails 中搭配 Turbolinks 使用 Vue
		
[Rails] Vue-outlet for Turbolinks 在踩了 Rails + Turbolinks + Vue 的許多坑後,整理 的作法並和大家分享. Initialize the A ...
 - Dockfile中的命令如何在.sh中执行
		
有类似如下内容的Dokefile文件.1 RUN cd /tmp/patch \ && /lib/python3./site-packages/moduleA/a.* \ && ...
 - 如何在cmd中执行python文件
		
打开cmd终端 输入python 然后再输入要执行文件的路径 就可以把python文件运行起来 ...
 - 关于如何在MVC中 执行JS
		
Response.Write("<script>KHTPREFERENCE()</script>"); return this.MessageResult( ...
 - 如何在java中发起http和https请求
		
一般调用外部接口会需要用到http和https请求. 一.发起http请求 1.写http请求方法 //处理http请求 requestUrl为请求地址 requestMethod请求方式,值为&qu ...
 - 如何在Java中调用Python代码
		
有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...
 - phpStorm如何在Console控制台执行php文本,而不是浏览器中
		
如何在Console控制台执行php文本 phpStorm默认会在浏览器中执行脚本 默认的配置 配置PHP脚本 扩展,配置项目运行
 
随机推荐
- http://jingyan.baidu.com/article/2009576193ee38cb0721b416.html
			
http://jingyan.baidu.com/article/2009576193ee38cb0721b416.html
 - BestCoder Round #71 (div.2)
			
数学 1001 KK's Steel 类似斐波那契求和 #include <cstdio> #include <cstring> #include <algorithm& ...
 - Shell 编程基础之变量和环境变量
			
一.变量赋值和引用 Shell 编程中,使用变量无需事先声明,同时变量的命名不惜遵循如下规则: 首个字符必须为字母(a-z,A-Z)或者_ 变量名中间不能有空格,可以使用_连接 不能使用其他表达符号 ...
 - iPad apple-touch-startup-image实现portrait和landscape
			
iPad apple-touch-startup-image实现portrait和landscape 为ipad制作web应用程序的启动画面时发现个问题,只能显示竖屏图,横屏图出不来,网上的朋友都说无 ...
 - WPF standard ComboBox Items Source Change Issue
			
Today I encountered an issue with the WPF standard CombBox where if the bound ItemsSource (collectio ...
 - HTML5与移动端web学习笔记
			
HTML5 提供了很多新的功能,主要有: 新的 HTML 元素,例如 section, nav, header, footer, article 等 用于绘画的 Canvas 元素 用于多媒体播放的 ...
 - 【wikioi】2822 爱在心中
			
题目链接 算法:Tarjan+dfs(最短路的都行,判连通而已) 先了解一下什么是Tarjan Tarjan算法用于求出图中所有的强连通分量. 转自NOCOW:点击打开链接 ============= ...
 - 【BZOJ2456】mode 神奇的卡内存题
			
以后把题解放在前面,估计没人看题解先看题... 内存1M,4个int(其实对内存的概念十分模糊),众数即为出现次数最多的数,可以用抵消的思想(但是众数不是可以是一大坨么...) #include &l ...
 - ThinkPHP随笔
			
使用应用分组模式开发程序时,U函数表示地址时,要用这种格式 “项目名/控制器名/方法名”写地址,不能直接使用U("handle") 如:var handleUrl = '{:U(& ...
 - TCP/IP基础知识
			
TCP/IP基础知识 网络 TCP/IP 引言 本篇属于TCP/IP协议的基础知识,重点介绍了TCP/IP协议簇的内容.作用以及TCP.UDP.IP三种常见网络协议相关的基础知识. 内容 TCP/IP ...