0

I've been trying for quite some time now to figure out why the JSON object I'm passing through AJAX to Rails with Typhoeus isn't working properly. Apologies if this is a newb question but I'm relatively new to web dev.

I've spent all day looking at docs, googling, and on SO but I haven't been able to figure out much for some reason.

I'm trying to pass a request to Google's QPX Express API for flight search and the docs say to send a JSON obj in the following format:

{
  "request": {
    "passengers": {
      "kind": "qpxexpress#passengerCounts",
      "adultCount": 1,
      "childCount": 0,
      "infantInLapCount": 0,
      "infantInSeatCount": 0,
      "seniorCount": 0
    },
    "slice": [
      {
        "kind": "qpxexpress#sliceInput",
        "origin": "SFO",
        "destination": "HNL",
        "date": "2015-04-03",
        "maxStops": 0,
        "maxConnectionDuration": 0,
        "preferredCabin": "COACH",
        "permittedDepartureTime": {
          "kind": "qpxexpress#timeOfDayRange",
          "earliestTime": "00:00",
          "latestTime": "11:59"
        },
        "permittedCarrier": [
          "VX",
          "UA"
        ],
        "alliance": "",
        "prohibitedCarrier": [
          ""
        ]
      }
    ],
    "maxPrice": "USD1000.00",
    "saleCountry": "US",
    "refundable": false,
    "solutions": 1
  }
}

I have this stored a variable which is referenced in the AJAX request below as 'reqBody':

$.ajax({
  url: '/search',
  dataType: 'json',
  contentType: 'application/json',
  method: 'POST',
  // contentType: "application/json; charset=utf-8",
  data: JSON.stringify(reqBody),
  success: function(data) {
    console.log(data);
  }
});

And this is call goes to the rails controller shown here, using Typhoeus to process the request/response:

reqBody = params[:request]
flightRequest = Typhoeus::Request.new(
  "https://www.googleapis.com/qpxExpress/v1/trips/search?key=APIKEY",
  method: :post,
  headers: {'Content-Type'=> "application/json; charset=utf-8"},
  body: reqBody,
)
flightRequest.run
@results = JSON.parse(flightRequest.response.body)
respond_to do |format|
  format.html
  format.json { render json: {
    :results => @results
    }
  }
end

This ends up being the response I get back:

{"results":{"error":{"errors":[{"domain":"global","reason":"parseError","message":"Parse Error"}],"code":400,"message":"Parse Error"}}}

And this is what I get when I look at the obj in pry:

=> {"passengers"=>
  {"kind"=>"qpxexpress#passengerCounts",
   "adultCount"=>1,
   "childCount"=>0,
   "infantInLapCount"=>0,
   "infantInSeatCount"=>0,
   "seniorCount"=>0},
 "slice"=>
  [{"kind"=>"qpxexpress#sliceInput",
    "origin"=>"SFO",
    "destination"=>"HNL",
    "date"=>"2015-04-03",
    "maxStops"=>0,
    "maxConnectionDuration"=>0,
    "preferredCabin"=>"COACH",
    "permittedDepartureTime"=>
     {"kind"=>"qpxexpress#timeOfDayRange", "earliestTime"=>"00:00", "latestTime"=>"11:59"},
    "permittedCarrier"=>["VX", "UA"],
    "alliance"=>"",
    "prohibitedCarrier"=>[""]}],
 "maxPrice"=>"USD1000.00",
 "saleCountry"=>"US",
 "refundable"=>false,
 "solutions"=>1}

What's going on here? Shouldn't the object be a string since I stringified it in the AJAX request? Is this why there's a parsing error when I send the object to the QPX Express API?

Any help is highly appreciated!

Thanks!

1 Answers1

0

Just a thought, did you require 'json' in your controller?

The other thing you could try is the variant of parse

parse! which can be used for safe safe sources.

here is a link!

How do I parse JSON with Ruby on Rails?

good luck

Community
  • 1
  • 1
MikePopsicle
  • 19
  • 1
  • 5