murz
murz

Reputation: 115

Unable to get XML data from rails controller

I'm having an issue grabbing the XML data from the controller from a POST request with XML. I'm able to parse the XML data fine in irb with Nokogiri once I have the XML stored in a string variable. But there doesn't seem to be any parameters with the XML data being supplied in the POST request. Any help to put me in the right direction for how to grab the XML data would be great!

Started POST "/vz_api" for 127.0.0.1 at 2014-06-24 10:59:07 -0400
Processing by LocationsController#vz_api as */*

Controller code

def vz_api
  require 'nokogiri'
  puts params # {"controller"=>"locations", "action"=>"vz_api"}
  xml_doc = Nokogiri::XML(params["_data"])
  puts xml_doc # <?xml version="1.0"?>
  vin = xml_doc.xpath("//VIN").inner_text
  @truck = Truck.find_by(vin: vin) if vin.present?
  lat = xml_doc.xpath("//Latitude").inner_text
  lng = xml_doc.xpath("//Longitude").inner_text
  @location = @truck.location
  respond_to do |format|
    if @location.update_attributes(longitude: lng, latitude: lat)
      format.xml { render xml: @location }
    else
      format.xml { render xml: @location.errors, status: :unprocessable_entity }
    end
  end
end

irb XML Parsing

f = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><NetworkfleetMessage Type="GPS"><VIN>1M2AXO4C5CM014389</VIN><FleetId>47790</FleetId><MsgId>25195315419</MsgId><MessageTime>2014-06-24 10:09:46 GMT</MessageTime><MessageTimeUTF>1403604586</MessageTimeUTF><DeliveryStatus>Current</DeliveryStatus><GPSFixes NumFixes="1"><GPSFix><FixTime>2014-06-24 10:08:59 GMT</FixTime><FixTimeUTF>1403604539</FixTimeUTF><Latitude>40.83705</Latitude><Longitude>-73.87563</Longitude><Ignition>On</Ignition><Speed Type="Avg" Units="MPH">0</Speed><Speed Type="Inst" Units="MPH">0</Speed><Speed Type="Max" Units="MPH">0</Speed><Odometer>51525.976</Odometer><AgeInMiles>0.000</AgeInMiles></GPSFix></GPSFixes></NetworkfleetMessage>'
2.0.0-p353 :003 > require 'nokogiri'
 => false
2.0.0-p353 :004 > doc = Nokogiri::XML(f)
 => #<Nokogiri::XML::Document:0x8270cf4c name="document" children=[#<Nokogiri::XML::Element:0x8270cbdc name="NetworkfleetMessage" attributes=[#<Nokogiri::XML::Attr:0x8270cb78 name="Type" value="GPS">] children=[#<Nokogiri::XML::Element:0x8270c5c4 name="VIN" children=[#<Nokogiri::XML::Text:0x8270c31c "1M2AXO4C5CM014389">]>, #<Nokogiri::XML::Element:0x8270c1a0 name="FleetId" children=[#<Nokogiri::XML::Text:0x82710638 "47790">]>, #<Nokogiri::XML::Element:0x82711f24 name="MsgId" children=[#<Nokogiri::XML::Text:0x82711c04 "25195315419">]>, #<Nokogiri::XML::Element:0x827119c0 name="MessageTime" children=[#<Nokogiri::XML::Text:0x82711768 "2014-06-24 10:09:46 GMT">]>, #<Nokogiri::XML::Element:0x827115ec name="MessageTimeUTF" children=[#<Nokogiri::XML::Text:0x827113a8 "1403604586">]>, #<Nokogiri::XML::Element:0x82711204 name="DeliveryStatus" children=[#<Nokogiri::XML::Text:0x82710ffc "Current">]>, #<Nokogiri::XML::Element:0x82710e80 name="GPSFixes" attributes=[#<Nokogiri::XML::Attr:0x82710e1c name="NumFixes" value="1">] children=[#<Nokogiri::XML::Element:0x8271096c name="GPSFix" children=[#<Nokogiri::XML::Element:0x82710688 name="FixTime" children=[#<Nokogiri::XML::Text:0x82710444 "2014-06-24 10:08:59 GMT">]>, #<Nokogiri::XML::Element:0x8271023c name="FixTimeUTF" children=[#<Nokogiri::XML::Text:0x82714620 "1403604539">]>, #<Nokogiri::XML::Element:0x82715f70 name="Latitude" children=[#<Nokogiri::XML::Text:0x82715c00 "40.83705">]>, #<Nokogiri::XML::Element:0x82715a84 name="Longitude" children=[#<Nokogiri::XML::Text:0x827158a4 "-73.87563">]>, #<Nokogiri::XML::Element:0x827156c4 name="Ignition" children=[#<Nokogiri::XML::Text:0x827154bc "On">]>, #<Nokogiri::XML::Element:0x82715318 name="Speed" attributes=[#<Nokogiri::XML::Attr:0x827152b4 name="Type" value="Avg">, #<Nokogiri::XML::Attr:0x827152a0 name="Units" value="MPH">] children=[#<Nokogiri::XML::Text:0x82714bc0 "0">]>, #<Nokogiri::XML::Element:0x8271497c name="Speed" attributes=[#<Nokogiri::XML::Attr:0x827148b4 name="Type" value="Inst">, #<Nokogiri::XML::Attr:0x827148a0 name="Units" value="MPH">] children=[#<Nokogiri::XML::Text:0x827183c4 "0">]>, #<Nokogiri::XML::Element:0x82719fbc name="Speed" attributes=[#<Nokogiri::XML::Attr:0x82719ecc name="Type" value="Max">, #<Nokogiri::XML::Attr:0x82719eb8 name="Units" value="MPH">] children=[#<Nokogiri::XML::Text:0x82719814 "0">]>, #<Nokogiri::XML::Element:0x82719670 name="Odometer" children=[#<Nokogiri::XML::Text:0x82719490 "51525.976">]>, #<Nokogiri::XML::Element:0x82719314 name="AgeInMiles" children=[#<Nokogiri::XML::Text:0x8271910c "0.000">]>]>]>]>]>
2.0.0-p353 :005 > doc.xpath("//VIN").inner_text
 => "1M2AXO4C5CM014389"

Upvotes: 0

Views: 400

Answers (1)

avalente1
avalente1

Reputation: 246

Try using the request object and read the body of the request and then parse that with Nokogiri

require 'nokogiri'
xml_doc = Nokogiri::XML(request.body.read)

Upvotes: 1

Related Questions