简体   繁体   English

将XML发布到外部API-在Rails中使用cURL

[英]Post XML to an external API - Using cURL in Rails

I've been working in the Rails console with Rails 3.2 and I'm generating an XML file using Nokogiri. 我一直在使用Rails 3.2在Rails控制台中工作,并且正在使用Nokogiri生成XML文件。 From here, I need to post to an external API to grab some data and return it within my app. 从这里,我需要发布到外部API来获取一些数据并在我的应用程序中将其返回。 Eventually this code will be a function of the controller, but for now I've been experimenting in the console. 最终,该代码将成为控制器的功能,但到目前为止,我一直在控制台中进行实验。

I generated and XML file with Nokogiri and parameters I specified, and I stored the output using the following command: 我使用Nokogiri和指定的参数生成了XML文件,并使用以下命令存储了输出:

File.open('results.xml', 'w') {|f| f.write(results)}

From here, I want to POST this file to the external API. 从这里,我想将此文件发布到外部API。 The command I used saved it in the /public directory of my app. 我使用的命令将其保存在应用程序的/ public目录中。 From here, I'm unsure of how to access it with cURL. 从这里,我不确定如何使用cURL访问它。

I tried putting it in a views directory and set up a route so I can GET the file, and I can at least access it. 我尝试将其放在views目录中并设置路由,以便可以获取文件,并且至少可以访问它。 Here is what I tried in cURL (note that the Rails server was running at the time and the API path below is made up for example purposes): 这是我在cURL中尝试过的内容(请注意,Rails服务器当时正在运行,下面的API路径仅供参考):

curl -X POST -v --data-ascii http://localhost:3000/search/postresults.xml http://APIPATH/example.php

This one has been frustrating me for awhile, and when I try that I get an error saying: 这已经让我感到沮丧了一段时间,当我尝试这样做时,我收到一条错误消息:

SyntaxError: (irb):5: syntax error, unexpected tCONSTANT, expecting keyword_do or '{'  or '('
curl -X POST -v --data-ascii http://local...
        ^
(irb):5: syntax error, unexpected tUMINUS, expecting keyword_do or '{' or '('
curl -X POST -v --data-ascii http://localhost:...
             ^
(irb):5: syntax error, unexpected tLABEL, expecting keyword_do or '{' or '('
...l 
-X POST -v --data-ascii http://localhost:3000/search/postr...
...                               ^
(irb):5: unknown regexp options - lcalht

I've tried all of the standard troubleshooting (curl is installed - version 0.0.9, server is running, curl is in my Gemfile, etc), so any help is much appreciated. 我已经尝试了所有标准的故障排除方法(已安装curl-0.0.9版,服务器正在运行,curl在我的Gemfile中,等等),因此非常感谢您的帮助。 Thanks! 谢谢!

Your errors suggest that you typed the curl command and arguments directly into IRb. 您的错误提示您直接在IRb中键入curl命令和参数。 That's not how Ruby gems work. 那不是Ruby gems的工作方式。 Furthermore, if you want to POST to an HTTP resource from Rails don't bother with cURL. 此外,如果您想从Rails张贴到HTTP资源,请不要担心cURL。 Rails has built-in tools for this. Rails为此提供了内置工具。

If you're going to be interacting with this API a lot, and if the API is fairly RESTy, then take a look at ActiveResource (tutorials abound on Google if the docs don't do it for you). 如果您打算与该API进行大量交互,并且该API是相当RESTy的,那么请看一下ActiveResource (如果文档不适合您,那么Google上会有很多教程)。

If you're not using a very RESTy API, or if this is a one-off API call, you can create an instance of ActiveResource::Connection directly, eg: 如果您使用的不是非常RESTy的API,或者这是一次性的API调用,则可以直接创建ActiveResource :: Connection的实例,例如:

conn = ActiveResource::Connection.new 'http://example.com'

result = conn.post 'example.php', results

There's likely no need to write the Nokogiri document ( results ) to a file, just give it directly to ActiveResource::Connection#post . 可能无需将Nokogiri文档( results )写入文件,只需直接将其提供给ActiveResource::Connection#post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM