如何在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脚本 扩展,配置项目运行
随机推荐
- Codeforces Round #336 (Div. 2)
水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...
- 旧版API的TextInputFormat源码分析
TextInputFormat类 package org.apache.hadoop.mapred; import java.io.*; import org.apache.hadoop.fs.*; ...
- Windows 8 系统安装
系统城 http://www.xitongcheng.com/win8/ 1. 下载 win8: http://msdn.itellyou.cn/2. 准备 4G 以上 U 盘,下载 win8 ...
- 通过反射将变量值转为变量名本身ZZ
这是.NET反射的一个有趣小例子: 通过反射将变量值转为变量名本身. 当然要先添加命名空间:using System.Reflection; 示例代码如下: class Program { st ...
- BZOJ2002 & LCT模板(分块不会搞)
题意: 看题. 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿 着一条直线摆上n个装置,每个装置设定初 ...
- android service 如何弹出dialog
在service中弹出toast,需要使用handler, handler.post(new Runnable() { @Override public void run() { Toast.make ...
- 【JAVA】Math.Round()函数常见问题“四舍5入”
java.lang.Math.Round()使用时候,处理方式整理,方便以后查找 /** * 测试函数 2014-01-10 */ public class TestMath { pu ...
- BZOJ3456: 城市规划
Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一些贸易路线, 使得整个国家的任意两个城市都直接或 ...
- Lable得到自定义高度!
方法1(系统):CGSize declabesize = [_questionDecLabel.text boundingRectWithSize:CGSizeMake(CGRectGetWidth( ...
- 如何使用命令行编译以及运行java文件
要想编译和运行java文件,很简单,只需要两个命令: (1) javac:作用:编译java文件:使用方法: javac Hello.java ,如果不出错的话,在与Hello.java 同一目录下会 ...