rails 3 doesn't render format xml for string - ruby-on-rails

so i'm trying to send xml back from my controller..
render xml: ['hello world']
correctly gives me:
<?xml version="1.0" encoding="UTF-8"?>
<strings type="array">
<string>hello world</string>
</strings>
however
render xml: 'hello world'
gives xml headers but the body is just:
hello world
which is not xml format.
bug?

From the API documentation:
When a request comes in, for example for an XML response, three steps
happen:
1) the responder searches for a template at people/index.xml;
2) if the template is not available, it will invoke
#to_xml on the given resource;
3) if the responder does not respond_to :to_xml, call
#to_format on it.
See: http://api.rubyonrails.org/classes/ActionController/Responder.html
In Rails, Arrays respond to to_xml but Strings do not.

Related

Ruby Nokogiri XML formatting for post request

I am setting up a method in a Rails app which posts 2 bits of data to an endpoint api.
Using Nokogiri to parse the data, and Faraday to send request, I have got very close to what is needed for successful post of multiple order items data, excepting a key issue.
This is that I am getting unwanted repeats of the top level node, because it is inside an each loop, which is needed for the items, whereas we only need that once. Below is the method, whereby an object is parsed and the order items put in to place in xml with Nokogiri and here we are just accessing the closed orders in the data object by definition, and just sending the id and quantity for each of them.
def update_backoffice_closed_orders(bj_update)
bj_update.each do |item|
if item['order_status'] == 'closed'
id = item['order_number']
del = item['quantity_fulfilled']
# Set up the connection to the Backoffice API and items to be updated
#xml_post = Nokogiri::XML::Builder.new do |xml|
xml.supplier {
xml.sid SID
xml.password PASSWORD
xml.order {
xml.id id
xml.volume_Delivered del
}
}
end
# Send the request to the Backoffice API
#response = CONNECTION.post do |req|
req.url '/suppliers/orderDataUpdate.php'
req.body = #xml_post.to_xml
end
end
end
end
This is working OK except failing at api because the top level nodes repeat like so:
<?xml version="1.0"?>
<supplier>
<sid>344</sid>
<password>4657gdhtey6</password>
in each order, whereas all are only needed once at top, with supplier closing tag after all the orders in between, as in the example below.
<?xml version="1.0"?>
<supplier>
<sid>344</sid>
<pw>4657gdhtey6</pw>
<order>
<id>3686414</id>
<volume_delivered>480</volume_delivered>
</order>
<order>
<id>3686425</id>
<volume_delivered>480</volume_delivered>
</order>
<order>
<id>3686443</id>
<volume_delivered>480</volume_delivered>
</order>
</supplier>
CONNECTION is a Faraday constant and works fine, as are SID and PASSWORD, if I can get the Nokogiri #xml_post like above, then it is exactly as needed and used in Postman tests for the api, and should work. If anyone knows how to adjust the xml builder to achieve that in this method, please share?
Biggest problem seems the <?xml opening tag which is default with Nokogiri.
Many Thanks

API response with HTTP 200 with an XML body

How can the HTTP 200 with an XML body be sent as the response of an API?
I am working on rails and the response of an API is in following format. It means like sending XML message:
<?xml version="1.0"?>
<s:Envelope>
<s:Body>
</s:Body>
</s:Envelope>
But I want to send HTTP 200 with an XML body instead of this XML Message.
Please suggest how can this be achieved?
In your controller method, do this if the request is successful
#some_response_object = Openstruct.new()
respond_to do |format|
format.xml { render xml: #some_response_object }
end
If you want to render a specific xml file, you simply use render, it'll try to find an xml file under the view folder which matches the file path.

Send XML request from Rails application. XML model payment implementation

I am working on the implementation of a payment system on my website. But as the payment system works according to the old model, all requests go through XML. I need to send XML (the code is attached below) to the URL and get a response. How could i send XML post request with Ruby on Rails. I need to send in to this url https://setmpos.ykb.com/PosnetWebService/XML?xmldata=%3CposnetRequest%3E%0D%0A++%3Cmid%3E... Where xmldata is query parametr.
<?xml version="1.0" encoding="ISO-8859-9"?>
<posnetRequest>
<mid>6700000067</mid>
<tid>67000067</tid>
<tranDateRequired>1</tranDateRequired>
<sale>
<amount>2451</amount>
<ccno>4506349116608409</ccno>
<currencyCode>TL</currencyCode>
<cvc>000</cvc>
<expDate>0703</expDate>
<orderID>1s3456z8901234567890123</orderID>
<installment>00</installment>
</sale>
</posnetRequest>

Access fields from an http response by parsing the response

I got an XML Response
<?xml version="1.0"?>
<List>
<type>MAILBOX</type>
<DOCS>
<DocId>39fece005f28qrn8000003r5</DocId>
<DocNo>123</DocNo>
</DOCS>
<DOCS>
<DocId>39fece005f28qrn8000003r6</DocId>
<DocNo>456</DocNo>
</DOCS>
</List>
I need to parse it so that I can use it and access DocId; what should be proper code to access docid?
You need to set up parsing on the HTTPRequest node, on the Response Message Parsing tab you select XMLNSC as the message domain, then on the output terminal you can use another Compute node and access the parts of the message as normal in ESQL, e.g.: InputBody.List.DOCS[1].DocId

Rails not getting params from HTTP request

I'm trying to POST the following data to a Rails server (running on WebRick) from Android.
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<data>
<email>email.test#name.com</email>
<password>APassw0rd</password>
<remember_me>1</remember_me>
</data>
Now, the funny thing is that these data never show up in the params field in the controller.
Webrick does not output any parsing error. (And I guess it would post an error if it received a POST with no data attached:
Started POST "/users/sign_in.xml" for 192.168.1.94 at 2012-12-14 17:33:20 +0100
Processing by Users::SessionController#create as XML
Completed 500 Internal Server Error in 22643ms
I also found no trace of the data in the request.env variable. Actually, I see no HTTP_BODY in the dump fields. How can one see the raw body of the request? Would webrick really not complain if it received a POST with no attached data?
request.env would show the data as an IO object so you wouldn't be able to see your xml directly.
request.raw_post should return the raw data.
For rails to try and parse your xml into the params hash directly you need to set the content type of the request to application/xml. The 'processing as xml' stuff means that rails will try to render an xml response and doesn't necessarily have any bearing on the format of the posted data

Resources