简体   繁体   English

在矩形中为单个请求设置标头和http参数

[英]Setting headers and http params for a single request in restangular

I am trying to use restangular for file upload post request , I want to achieve the same functionality as below in restangular. 我正在尝试使用restangular进行文件上传后请求,我想在restangular中实现与以下相同的功能。 However, I was unsure how to set content type and transformRequest for just this particular request. 但是,我不确定如何仅为此特定请求设置内容类型和transformRequest。 If I understand correctly, setDefaultHeader sets it for all subsequent requests. 如果我理解正确,则setDefaultHeader会为所有后续请求设置它。 Is there some other way? 还有其他方法吗?

myApp.service('$fileUpload', ['$http', function ($http) {
   this.uploadFileToUrl = function(file, uploadUrl){
      var filedata = new FormData();
      filedata.append('file', file);
      $http.post(uploadUrl, filedata, {
         transformRequest: angular.identity,
         headers: {'Content-Type': undefined}
      })
      .success(function(){
      })
      .error(function(){
      });
     }
}]);

You have 2 situations here, the POST for create a new item or the PUT to edit an item: 您在这里有两种情况,用于创建新项目的POST或用于编辑项目的PUT:

// Save new Item
$scope.saveNew = function (item) {

  var data = new FormData();
  angular.forEach(item, function (fieldData, field) {
    data.append(field, fieldData);
  });

  Restangular
    .all('items')
    .withHttpConfig({transformRequest: angular.identity})
    .post(data, {}, {'Content-Type': undefined})
    .then(function () {
      // do on success
    }, function () {
      // do on failure
    });
};

// Edit existing Item
$scope.save = function (item) {

  var data = new FormData();
  angular.forEach(item.plain(), function (fieldData, field) {
    data.append(field, fieldData);
  });

  Restangular
    .one('items', item._id)
    .withHttpConfig({transformRequest: angular.identity})
    .customPUT(data, undefined, {}, {'Content-Type': undefined})
    .then(function () {
      $location.path('sites');
    });

To set the headers for a single request all you'll need to do is add an object containing the name and value of the headers as an argument to .post(), .get() or whatever method you need. 要为单个请求设置标头,您要做的就是添加一个包含标头名称和值的对象作为.post()、. get()或所需任何方法的参数。

https://github.com/mgonto/restangular#element-methods https://github.com/mgonto/restangular#element-methods

Restangular.all('some-endpoint').post(postContent, {}, {'Content-Type': undefined}).then(function (response) {
    console.log('Weeeeee!!!');
});

As for the transformRequest I am unsure of, I haven't had to deal with anything like that before, this is the only thing I could find on it in the documentation: 至于我不确定的transformRequest,我以前不必处理类似的事情,这是我在文档中唯一可以找到的东西:

https://github.com/mgonto/restangular#setdefaulthttpfields https://github.com/mgonto/restangular#setdefaulthttpfields

But that seems to set it for all the request which isn't what you want, but it's something at least. 但这似乎为所有不是您想要的请求设置了它,但至少是这样。

Anyway, hopefully this will help you get what you want. 无论如何,希望这将帮助您获得所需的东西。

Edit: 编辑:

Since most of the request types in restangular have a query param and then the headers you need to pass in a blank query param object and then the headers, example has been updated to show this. 由于大多数矩形中的请求类型都有一个查询参数,然后您需要在一个空白的查询参数对象中传递标头,然后再传递标头,因此示例已更新为显示此参数。

Since this is the first hit on Google for this issue, see Issue 420 in the Restangular issue tracker. 由于这是该问题在Google上的首个热门,因此请参阅Restangular问题跟踪器中的Issue 420

Basically the newest Restangular has a withHttpConfig function to set $http options right before a request is dispatched. 基本上,最新的Restangular具有withHttpConfig函数,可在分派请求之前立即设置$http选项。

If you have a route at a URL something like example.com/api/users/:id/picture that accepts a multipart upload with an image for a specific user you could do something like: 如果您在URL处有一条路由,例如example.com/api/users/:id/picture ,它接受特定用户的带有图像的分段上传,则可以执行以下操作:

Users.one(2)
    .withHttpConfig({transformRequest: angular.identity})
    .customPOST(filedata, 'picture', undefined, {'Content-Type': undefined})
    .then(function(resp) {
        // File data post is complete here
    });

By default Angular will transform any data sent with $http to JSON. 默认情况下,Angular会将使用$http发送的所有数据转换为JSON。 The transformRequest configuration simply replaces that default transformation with a NOP . transformRequest配置仅用NOP替换该默认转换。

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

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