发出 JSON RPC API 的 GET 请求,而不是 POST 请求



是否可以向 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&params=%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&params={"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-requesthttp://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 请求额外处理,则实际上并不取决于您和您的实现 - 这是必须设置服务器才能处理的事情。

最新更新