如何在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脚本 扩展,配置项目运行
随机推荐
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- 关于listView 中的聚焦问题
我在使用listView+adapter 中,遇到一个问题,就是item项添加了若干个可以被监听的控件后 在listView中的setOnItemClickListener失效了 原因是焦点已经在it ...
- java基础内容
此文作java语法基础的起始页,负责总结和目录引导 想我开始接触java语法也有比较长时间了(大概是2015年十月份),到现在(2016-10-25)为止还未系统的学习总结基础语法,亡羊补牢吧,回过头 ...
- Handling events in an MVVM WPF application
Posted: June 30, 2013 | Filed under: MVVM, WPF, XAML |1 Comment In a WPF application that uses the ...
- 编程之路 - 写给打算进入IT行业的新人们
IT=挨踢,这是IT人的自嘲,进入IT行业是有四五年了,也算得上是一个“老人”了吧,见了不少新人,面试了不少新人,也带了一些新人,多多少少还是有点发言权的. 关于书本 新人们常常会说我看了多少多少的书 ...
- 测试你是否和LTC水平一样高[HDU1407]
测试你是否和LTC水平一样高Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Target:IG
https://www.zhihu.com/question/25525630 别人轻轻松松红名,我拼死挣扎才1700+分. 仔细想想,虽然我在这东西上花了太多的精力,可是我根本没有认真学.做题全靠抄 ...
- BZOJ1481 : Navigation Game
设$f[i][j][k]$表示从最后一行某个$H$走到$(i,j)$且在第$i$行只经过了$(i,j)$,途中经过了$k$次$F$的最小代价. $A[i][j][k]$表示从下一行$\leq i$的某 ...
- BZOJ2320 : 最多重复子串
本题就是求重复数最多的字典序最小的$runs$,如果重复数为1,那么做法显然,然后只考虑重复数大于1的情况. 从小到大枚举长度$len$,对于每个关键点$x=i\times len$,有且仅有一个长度 ...
- div+css定位position详解
div+css定位position详解 1.div+css中的定位position 最主要的两个属性:属性 absolute(绝对定位) relative(相对定位),有他们才造就了div+css布局 ...