如何在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脚本 扩展,配置项目运行
随机推荐
- POJ1769 Minimizing maximizer(DP + 线段树)
题目大概就是要,给一个由若干区间[Si,Ti]组成的序列,求最小长度的子序列,使这个子序列覆盖1到n这n个点. dp[i]表示从第0个到第i个区间且使用第i个区间,覆盖1到Ti所需的最少长度 对于Si ...
- XIII Open Cup named after E.V. Pankratiev. GP of Saratov
A. Box Game 注意到局面总数不超过$50000$,而且每次操作都会改变石子的奇偶性,因此按奇偶可以将状态建成二分图,然后求出最大匹配. 如果状态数是偶数,那么先手必胜,策略就是每次走匹配边, ...
- 【BZOJ1984】月下“毛景树” 树链剖分+线段树
[BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...
- .htaccess
一.在Apache配置中启用Rewrite 打开配置文件httpd.conf: 1.启用rewrite# LoadModule rewrite_module modules/mod_rewrite.s ...
- -webkit-text-size-adjust:none;
- topcoder SRM 622 DIV2 BoxesDiv2
注意题目这句话,Once you have each type of candies in a box, you want to pack those boxes into larger boxes, ...
- xenu工具介绍
Xenu Link Sleuth 也许是你所见过的最小但功能最强大的检查网站死链接的软件了.你可以打开一个本地网页文件来检查它的链接,也可以输入任何网址来检查.它可以分别列出网站 的活链接以及死链接, ...
- C语言break和continue
break和continue C语言中有有两种结束循环的关键字:break和continue #include <stdio.h> #include <stdlib.h> in ...
- Sping Environment为Null的原因和解决方法
参考:https://github.com/spring-projects/spring-boot/issues/4711 这个issue提出不到20天给我搜出来了,还是相信google的强大 问题: ...
- 采用DOM进行表格的修改操作
2015-08-31 <html> <head> <title>采用DOM进行表格的修改操作</title> <script language=& ...