在 Meteor 中,我正在向外部 API 发出请求(这个 - http://sciencesoft.at/latex/?lang=en)。
从客户端(控制台)执行 Meteor.http.call 时,它可以正常工作。从服务器执行相同操作时,Meteor.http.call 似乎发送了一个空主体的调用(忽略其选项参数)。
我使用的完整且具体的代码写在下面。这里我尝试指出问题的原理:
在客户端(在控制台中)这工作正常:
Meteor.http.call('PUT', 'http://sciencesoft.at/latex', {content: sentxml}, function (e,r) {console.log(r.content)}); //asynchronously
这里 sentxml 包含 xml 请求的主体(在外部 API 的文档中指定)。
在服务器上,我有一个 Meteor.method,其中包含:
return Meteor.http.call('PUT', 'http://sciencesoft.at/latex', {content: sentxml}); //synchronously
当我在控制台中这样做时:
Meteor.call('myMethod', sentxml, function (e,r) {
console.log(r.content);
});
我返回一个包含错误的 xml 响应(与我在方法中省略 Meteor.http.call() 的第三个参数相同的错误)。
更具体地说,完整的代码如下。
当我在控制台中这样做时:
src= 'ABCD'; // I want to get png of this text.
latexsrc = '\\documentclass[12pt]{article}\\pagestyle{empty}\\begin{document}'+ src +'\\end{document}'; // minimalistic LaTeX source code
sentxml = '<?xml version="1.0" encoding="UTF-8"?><latex ochem="false"><dev dpi="120">png16m</dev><src><![CDATA['+latexsrc+']]></src><embeddedData>true</embeddedData></latex>'; // body of the xml request as described in the docs of http://sciencesoft.at/latex/?lang=en
Meteor.http.call('PUT','http://sciencesoft.at/latex', {content: sentxml}, function (e,r) {console.log(r.content)});
然后将 sentxml 正确发送到指定的 url,我得到一个正确的 xml 响应,其中包含我请求的数据。
但是,我想从服务器进行 http 调用。我在 Meteor 方法中有与上面几乎相同的代码:
if (Meteor.isServer) {
Meteor.methods({
getLatexImgData: function (src) {
this.unblock();
var latexsrc = '\\documentclass[12pt]{article}\\pagestyle{empty}\\begin{document}'+ src +'\\end{document}';
var sentxml = '<?xml version="1.0" encoding="UTF-8"?><latex ochem="false"><dev dpi="120">png16m</dev><src><![CDATA['+latexsrc+']]></src><embeddedData>true</embeddedData></latex>';
var result = Meteor.http.call('PUT','http://sciencesoft.at/latex', {content: sentxml});
return result;
}
});
}
当我现在在控制台中执行以下操作时:
src = 'ABCD';
Meteor.call('getLatexImgData', src, function (e,r) {
console.log(r.content);
});
然后 r.content 包含一个带有错误消息的 xml 响应“元素 'src' 为空!没有可用的 LaTeX 源!” (与 http.call 使用空正文发出的消息相同)。