发布于 2016-04-27 22:01:00
在阅读了tadman上面的答案之后,我更仔细地研究了将数据直接添加到HTTP请求的主体。最后,我正是这样做的:
require 'uri'
require 'json'
require 'net/http'
jsonbody = '{
"id":50071,"name":"qatest123456","pricings":[
{"id":"dsb","name":"DSB","entity_type":"Other","price":6},
{"id":"tokens","name":"Tokens","entity_type":"All","price":500}
]
}'
# Prepare request
url = server + "/v1/entities"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.set_debug_output( $stdout )
request = Net::HTTP::Put.new(uri )
request.body = jsonbody
request.set_content_type("application/json")
# Send request
response = http.request(request)如果您想调试发送的HTTP请求,请使用以下代码:http.set_debug_output( $stdout )。这可能是调试通过Ruby发送的HTTP请求的最简单方法,并且非常清楚发生了什么:)
发布于 2016-04-27 19:57:21
虽然使用像法拉第这样的东西通常要愉快得多,但是它仍然可以使用Net::HTTP库:
require 'uri'
require 'json'
require 'net/http'
url = URI.parse("http://example.com/endpoint")
http = Net::HTTP.new(url.host, url.port)
content = { test: 'content' }
http.post(
url.path,
JSON.dump(content),
'Content-type' => 'application/json',
'Accept' => 'text/json, application/json'
)https://stackoverflow.com/questions/36899600
复制相似问题