0

I am calling the JDoodle API with post request from my local machine Rails server with valid id and secrete. I am not getting desired response. Please suggest me if i am doing wrong....

My Ruby function to make api call

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

And the response is

{"error":"Unauthorized Request","statusCode":401}
Raja Sharma
  • 460
  • 1
  • 7
  • 19

2 Answers2

1

try changing this line:

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

to this:

request.content_type = "application/json" 
Julien
  • 2,217
  • 2
  • 28
  • 49
0

I changed the code as below and it worked but can't say why.?

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`
Raja Sharma
  • 460
  • 1
  • 7
  • 19