设置“内容类型” Meteor中客户端的HTTP.call中的标头 [英] Setting the "Content-Type" header in HTTP.call on client side in Meteor

查看:145
本文介绍了设置“内容类型” Meteor中客户端的HTTP.call中的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Meteor(v1.0) HTTP.call 方法与基于Python的服务器进行通信,该服务器只接受应用程序/ json 标题中的内容类型,但是当从客户端调用API URL时,我无法在Meteor中正确设置HTTP标头。

I'm trying to use Meteor's (v1.0) HTTP.call method to communicate with a Python-based server which accepts only application/json content type in header but I cannot set the HTTP header properly in Meteor when calling the API URL from a client side.

像这样的片段,我从Python服务器得到 415(不支持的媒体类型)错误:

With a snippet like this, I get a 415 (Unsupported Media Type) error from Python server:

if (Meteor.isClient) {
  Template.testing.events({
    'click button': function(event, tpl) {
      event.preventDefault();

      var method = 'GET';
      var url = 'http://localhost:6543/test';
      var options = {
        headers: {'Content-Type': 'application/json'}
      }

      HTTP.call(method, url, options, function(error, result) {
        if (error) {
          console.log('ERRR');
          console.log(error);
        } else
          console.log('RESULT');
          console.log(result);
      });
    }

  });
}

但是,如果我在Meteor中从服务器端调用相同的URL,就像这样:

However, if I call the same URL from a server side in Meteor like this:

if (Meteor.isClient) {
  Template.testing.events({
    'click button': function(event, tpl) {
      event.preventDefault();

      var method = 'GET';
      var url = 'http://localhost:6543/test';
      var options = {
        headers: {'Content-Type': 'application/json'}
      }

      Meteor.call('APICall', method, url, options, function (error, result) {
        if (error) {
          console.log('CLIENT ERRR');
          console.log(error);
        } else {
          console.log('CLIENT RESULT');
          console.log(result);
        }
      });
    }

  });
}

if (Meteor.isServer) {
  Meteor.methods({
    APICall: function (method, url, options) {
      HTTP.call(method, url, options, function(error, result) {
        if (error) {
          console.log('SERVER ERRR');
          console.log(error);
        } else
          console.log('SERVER RESULT');
          console.log(result);
      });
    }
  });
}

我从服务器得到了正确的答复。

I get a proper response from the server.

在Python方面,我为所有可能的请求启用了CORS起源(例如 cors_origins =('*'))。

On Python side I enabled CORS origins for all possible requests (e.g. cors_origins=('*')).

那么......可以在客户端设置标头,还是应该总是从服务器端调用此服务?

So... Is is possible to set header on client-side or should I always call this service from server-side?

推荐答案

我在客户端也没有取得任何成功,但它应该是。查看meteor HTTP包的HTTP.call客户端部分:

I've never had any success on the client-side either, but it is supposed to. Check out the HTTP.call client part of the meteor HTTP package:

https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_client.js

大多数情况下,它使用客户端的浏览器XHR对象,这可能导致许多问题,如不兼容性和内容。您甚至可以看到其中一个代码注释引用的问题(围绕第136行

Mostly it uses the browser XHR object on the client-side which can lead to a host of problems, like incompatibilities and stuff. You can even see an issue referenced on one of the code comments (around line 136)

当您查看服务器实现时,您可以看到它使用请求库(来自连接),在我的书中,它是非常可靠的,你可以在所有用户之间生成统一的结果(而不是围绕浏览器差异)。

And when you check out the server implementation you can see it uses the request library (from connect), which, in my book, is very reliable and you can generate uniform results across all users (and not dance around browser differences).

我的选择和推荐显然是服务器端调用。不仅因为它的工作原理而且它是可靠的,而且您也可以更安全,因为您不必将更多系统内部工作暴露给客户端/最终用户。谁知道?也许你在基于Python的服务器上运行的API上有敏感数据。

My choice and recommendation for you is obviously going to be server-side calls. Not just because it works and it's reliable, it's also 'safer' on your part as you don't have to expose more inner workings of your system to the client/end-user. Who knows? maybe you have sensitive data on your API run on your Python-based server.

这篇关于设置“内容类型” Meteor中客户端的HTTP.call中的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆