2017-02-09 289 views
3

是否可以向JSON RPC API發出GET請求?我正在嘗試使用random.org api(https://api.random.org/json-rpc/1/)來做到這一點。如果我使用POST請求,它會運行,但我需要爲我正在使用的應用程序中使用的所有API發出GET請求。製作JSON RPC API而不是POST請求的GET請求

這裏是POST請求被工作:

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

我試着成立URL沒有運氣以下幾種方式:

用URL編碼的PARAMS:

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' 

我缺少什麼?提前致謝!

+0

每個文檔鏈接出現您需要base64編碼參數 – charlietfl

回答

0

來自紐約時報集會小組的一些友人幫我解答了這個問題。對於任何人誰發現這個問題:

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請求,那麼這不是真的取決於您和您的實現 - 這是服務器必須設置才能處理的事情。」