0

我正在使用来自本地机器 Rails 服务器的带有有效 ID 和秘密的发布请求调用 JDoodle API。我没有得到想要的回应。如果我做错了,请建议我....

我的 Ruby 函数进行 api 调用

def run_Jddodle_API

        require 'net/http' 
        require 'uri' 
        require 'json' 
        uri = URI.parse("https://api.jdoodle.com/v1/execute") 
        request = Net::HTTP::Post.new(uri) 
        request.content_type = "application/json; charset=UTF-8" 
        request.body = { 
            "clientId" => "ddc371fd*************c8efbae", 
            "clientSecret" => "4ee8e79a225***************************a8ee7f331aeeca603", 
            "script" => "<?php printf(\"hello RAJA\"); ?>", 
            "language" => "php", 
            "versionIndex" => "0" 
        }.to_json
        req_options = { use_ssl: uri.scheme == "https", } 

        response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| 
            http.request(request) 
        end 
        puts response.body
    end

回应是

{"error":"Unauthorized Request","statusCode":401}
4

2 回答 2

1

尝试更改此行:

request.content_type = "application/json; charset=UTF-8" 

对此:

request.content_type = "application/json" 
于 2019-04-23T20:34:23.253 回答
0

我改变了代码如下,它工作但不能说为什么。?

require 'uri'
require 'net/http'
require 'net/https'


url = URI("https://api.jdoodle.com/v1/execute")

          http = Net::HTTP.new(url.host, url.port)
          http.use_ssl = true

          request = Net::HTTP::Post.new(url.path)
          request["Content-Type"] = 'application/json'
          request.body = {
            "script" => params[:code],
            "language" => params[:lang],
            "versionIndex" => params[:version],
            "clientId" => "dc37******************efbae",
            "clientSecret" => "4ee8e79a225a5525*******************************************"
          }.to_json
          response = http.request(request)
          puts response.read_body`
于 2019-04-24T17:58:03.023 回答