是否可以向JSON RPC API发出GET请求?我尝试使用random.org api(https://api.random.org/json-rpc/1/)来做这件事。如果我使用POST请求,它可以正常运行,但我需要为我正在处理的应用程序中使用的所有API发出GET请求。
以下是有效的帖子请求:
function getNewThing(apiUrl) {
data = {
"jsonrpc": "2.0",
"method": "generateIntegers",
"params": {
"apiKey": "9b6ed250-67fc-4afd-b60b-09c6076e5178",
"n": 1,
"min": 0,
"max": 1000000,
"replacement": true,
"base": 10
},
"id": 683489
}
// ajax call to the api
return $.ajax({
type: "POST",
url: apiUrl,
data: JSON.stringify(data),
success: function(result) {
console.log(result)
},
error: function(err) {
console.log(err)
}
});
}
我认为可以将其转换为GET请求的原因是因为此文档暗示它可以是:http://www.jsonrpc.org/historical/json-rpc-over-http.html#encoded-parameters
我试图通过以下方式设置网址,但没有运气:
使用params的URL编码:
https://api.random.org/json-rpc/1/invoke?jsonrpc=2.0&method=generateIntegers¶ms=%7B%22apiKey%22%3A%20%229b6ed250-67fc-4afd-b60b-09c6076e5178%22%2C%22n%22%3A%201%2C%22min%22%3A%200%2C%22max%22%3A%201000000%2C%22replacement%22%3A%20true%2C%22base%22%3A%2010%7D&id=683489
没有:
'https://api.random.org/json-rpc/1/invoke?jsonrpc=2.0&method=generateIntegers¶ms={"apiKey": "9b6ed250-67fc-4afd-b60b-09c6076e5178","n": 1,"min": 0,"max": 1000000,"replacement": true,"base": 10}&id=683489'
我错过了什么?提前谢谢!
答案 0 :(得分:0)
来自NYJS聚会小组的一些人帮我解决了这个问题。对于任何发生的人都会发现这个问题:
POST是你唯一的选择,因为非常不鼓励GET。 https://www.simple-is-better.org/json-rpc/transport_http.html#get-request http://www.jsonrpc.org/historical/json-rpc-over-http.html#http-header
对于random.org API,POST是唯一的选择 https://api.random.org/json-rpc/1/introduction
所有调用必须通过HTTP POST进行。特别是,不支持HTTP GET。
请注意:“如果POST请求可以作为GET请求另外处理,那么这并不取决于您和您的实现 - 这是服务器必须设置才能处理的事情。”