如何使用流星进行API调用并将header传递给它
I am trying to call one API using Meteor. The sample code given in API guide is as below:
我想用流星命名一个API。API指南给出的示例代码如下:
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userName + password)));
I have to use this code in Meteor, but I have not found any solution yet. I was able to call API which has no header using both GET
and POST
method. But how to pass above header to that API call? I am using Meteor.http.post
to make an API call. I am using following code by referring above code:
我必须在《流星》中使用这个代码,但是我还没有找到任何解决方案。我可以使用GET和POST方法调用没有头的API。但是如何将上面的header传递给API调用呢?我用Meteor.http。发布以进行API调用。我正在使用以下代码通过参考上述代码:
return Meteor.http.post("url",
{ headers: {"Authorization": "Basic"+(new Buffer(username+password, "ascii")).toString("base64")},
params: { name: username} } );
But it's not working. I also have working PHP code sample to make this API call:
但这不是工作。我还有一个PHP代码示例来进行这个API调用:
$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', $additionalHeaders));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $username . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
Can anyone give me any hint about how to do it? Thank you in advance.
谁能给我点建议吗?提前谢谢你。
1 个解决方案
#1
1
callApi: function () {
return Meteor.http.post("url",
{ headers: {"Authorization": "Basic"+(new Buffer(username+password, "ascii")).toString("base64")},
params: { name: username} } );
}
Using above code in server side code, we can pass headers to an API.
在服务器端代码中使用上述代码,我们可以将头信息传递给API。
更多相关文章
- 利用javascript实现遍历xml文件的代码实例
- Objective-C方法/函数调用。 (来自javascript示例)
- 有一个简单但有用的jquery.JsPlumb示例吗?
- javascript 的MD5代码备份,跟java互通
- 这些年,我收集的JavaScript代码(二)
- 确保代码在*之后执行*对监视属性的更改已在UI中生效
- 如何用NodeJS组织构建、服务器、客户端和共享JavaScript代码
- 如何在Node中创建可重用的函数而不编写样板代码
- arcgis api for js入门开发系列十 自定义Navigation控件样式风格