我想在ruby中创建POST request。但是回去吧
#<Net::HTTPUnsupportedMediaType:0x007f94d396bb98>
我尝试的是:
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
auto_index_nodes =URI('http://localhost:7474/db/data/index/node/')
request_nodes = Net::HTTP::Post.new(auto_index_nodes.request_uri)
http = Net::HTTP.new(auto_index_nodes.host, auto_index_nodes.port)
request_nodes.add_field("Accept", "application/json")
request_nodes.set_form_data({"name"=>"node_auto_index",
"config" => {
"type" => "fulltext",
"provider" =>"lucene"} ,
"Content-Type" => "application/json"
})
response = http.request(request_nodes)
试图写这部分:
"config" => {
"type" => "fulltext",
provider" =>"lucene"} ,
"Content-Type" => "application/json"
}
像那样:
"config" => '{
"type" => "fulltext",\
"provider" =>"lucene"},\
"Content-Type" => "application/json"\
}'
这个尝试也没有帮助:
request_nodes.set_form_data({"name"=>"node_auto_index",
"config" => '{ \
"type" : "fulltext",\
"provider" : "lucene"}' ,
"Content-Type" => "application/json"
})
答案 0 :(得分:3)
试试这个:
require 'rubygems'
require "net/http"
require "uri"
require "json"
uri = URI.parse("http://localhost:7474/db/data/index/node/")
req = Net::HTTP::Post.new(uri.request_uri)
req['Content-Type'] = 'application/json'
req['Accept'] = 'application/json'
req.body = {
"name" => "node_auto_index",
"config" => { "type" => "fulltext", "provider" => "lucene" },
}.to_json
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
这是the API doc and a short introduction to Net::HTTP
Content-Type
和Accept
是标题,因此您需要将它们作为标题发送,而不是在正文中。 JSON内容应该放在请求正文中,但您需要将Hash转换为JSON,而不是将其作为名称/值对中的表单数据发送。