我正在尝试在 ruby 中发出此POST 请求。但回来
#<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)
Run Code Online (Sandbox Code Playgroud)
尝试写这部分:
"config" => {
"type" => "fulltext",
provider" =>"lucene"} ,
"Content-Type" => "application/json"
}
Run Code Online (Sandbox Code Playgroud)
像那样:
"config" => '{
"type" => "fulltext",\
"provider" =>"lucene"},\
"Content-Type" => "application/json"\
}'
Run Code Online (Sandbox Code Playgroud)
这次尝试也没有帮助:
request_nodes.set_form_data({"name"=>"node_auto_index",
"config" => '{ \
"type" : "fulltext",\
"provider" : "lucene"}' ,
"Content-Type" => "application/json"
})
Run Code Online (Sandbox Code Playgroud)
尝试这个:
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
Run Code Online (Sandbox Code Playgroud)
和是标头,因此您需要将它们作为标头发送,而不是在正文中发送Content-Type。AcceptJSON 内容应放入请求正文中,但您需要将哈希转换为 JSON,而不是将其作为名称/值对中的表单数据发送。
| 归档时间: |
|
| 查看次数: |
763 次 |
| 最近记录: |