如何在Meteor.js中使用二进制正文发出POST请求

时间:2021-04-11 06:59:15

I'm hoping to find a solution for a frustrating problem in Meteor.js. I want to make some API calls to an open API service for face detection. I just need to send an image and receive a JSON object. I cannot find a way to send an image in the post body with meteor.js. The default http package in Meteor has the following method

我希望在Meteor.js中找到一个令人沮丧的问题的解决方案。我想对一个开放的API服务进行一些API调用以进行面部检测。我只需要发送一个图像并接收一个JSON对象。我找不到用meteor.js在帖子体内发送图像的方法。 Meteor中的默认http包具有以下方法

HTTP.post(url, [callOptions], [asyncCallback])

HTTP.post(url,[callOptions],[asyncCallback])

or

HTTP.call(method, url, [options], [asyncCallback])

with the options

有选项

[options]  
 data Object
    JSON-able object to stringify and use as the HTTP request body. Overwrites content.

My question is how to attach a binary stream to the post body.

我的问题是如何将二进制流附加到帖子正文。

any help? Thanks

任何帮助?谢谢

1 个解决方案

#1


I don't think you can do this with Meteor HTTP.call

我不认为你可以用Meteor HTTP.call做到这一点

if you look at their source code https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_client.js You will find this

如果你看看他们的源代码https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_client.js你会发现这个

content = JSON.stringify(options.data);

To send binary data, you have to use XMLHttpRequest and FileReader API

要发送二进制数据,您必须使用XMLHttpRequest和FileReader API

I wrote an answer to a related topic at Meteor: uploading file from client to Mongo collection vs file system vs GridFS (using Option 2)

我在Meteor上写了一个相关主题的答案:从客户端上传文件到Mongo集合vs文件系统vs GridFS(使用选项2)

#1


I don't think you can do this with Meteor HTTP.call

我不认为你可以用Meteor HTTP.call做到这一点

if you look at their source code https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_client.js You will find this

如果你看看他们的源代码https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_client.js你会发现这个

content = JSON.stringify(options.data);

To send binary data, you have to use XMLHttpRequest and FileReader API

要发送二进制数据,您必须使用XMLHttpRequest和FileReader API

I wrote an answer to a related topic at Meteor: uploading file from client to Mongo collection vs file system vs GridFS (using Option 2)

我在Meteor上写了一个相关主题的答案:从客户端上传文件到Mongo集合vs文件系统vs GridFS(使用选项2)