是否可以向 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": "key",
"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
我尝试通过以下方式设置 URL,但没有成功:
参数的 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'
我错过了什么?提前致谢!
最佳答案
来自 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
All invocations must be made via HTTP POST. In particular, HTTP GET is not supported.
注释:“如果 POST 请求可以作为 GET 请求另外处理,这并不完全取决于您和您的实现 - 这是必须设置服务器才能处理的内容。”
关于javascript - 发出 JSON RPC API 的 GET 请求而不是 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42147836/