Sinatra JSON response looks accurate in WEBrick but doesn't work in Passenger/Apache

132 views Asked by At

So I have a small piece of Ruby/Sinatra code that returns a JSON as the response. The code works and the result is accurate in WEBrick, but in my Passenger/Apache setup the message is blank for the same code. Here is my code and response from WEBrick and Passenger/apache

Here is my code

require 'sinatra/base'
require 'json'
require 'openssl'

class MyApp < Sinatra::Base
  before do
    content_type :json
  end
  post "/" do
    payload = JSON.parse(request.body.read, :symbolize_names => true)
    raw = payload[:pem]
    pem = OpenSSL::X509::Certificate.new raw    
    File.open("cert.cer", "wb") { |f| f.print pem }
    message = %x[ruby ../do-something.rb cert.cer]
    {subject: sub_CN, message: message, success: "1" }.to_json
  end

And the output when this is run in WEBrick is

{
"subject": "mail.google.com",
"message": "This certificate is good\tcurrently valid.\nIt appears the cert 
 will expire in\t2 months.\n",
"success": "1"
 }

And the output from Passenger/Apache is

{
    "subject": "mail.google.com",
    "message": "",
    "success": "1"
}

please keep in mind the message contains \t and \n. Do these mess with Apache or Passenger?

1

There are 1 answers

0
Hongli On

As others have mentioned, the behavior you're seeing is most likely because the command %x[ruby ../do-something.rb cert.cer] failed for some reason. If it failed then it should have printed something to stderr, which ends up in the global Apache log file. Please look in that file and check whether you can find the relevant error messages.