如何模拟与角卷曲命令 [英] How to simulate curl command with Angular

查看:267
本文介绍了如何模拟与角卷曲命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个curl命令,我想用角来模拟:

 卷曲-k -F fieldName=@data.json -u用户名:密码网址

目前,我去这样做角后。然而,我碰上认证的问题。有没有参数我把用户ID和密码。

角code:

  $ scope.postCall =功能(){
        $ scope.ngResult =点击        VAR paramsJson = {
            imessageIdT:$ scope.messageIdT,
            ilobT:$ scope.lobT,
            iregionIdT:$ scope.regionIdT,
            iassetClassT:$ scope.assetClassT,
            additionalInfoT:$ scope.additionalInfoT        };      无功配置= {
        paramsJson:paramsJson
      };      $ http.post(简称网站,paramsJson,配置)
        .success(功能(数据,状态,头,配置)
        {
          $ scope.ngResult = logResult(POST SUCCESS,数据,身份,头,配置);
          //$scope.ngResult =是;
        })
        .error(功能(数据,状态,头,配置)
        {
          $ scope.ngResult = logResult(POST ERROR,数据,身份,头,配置);          //$scope.ngResult =否;
        });
    };


解决方案

假设基本身份验证,没有经过测试,这可能工作:

  VAR用户名=...,密码=***;
无功配置= {
    标题:{
        授权:基本+ window.btoa(用户名+:+密码)
    },
    方法:GET,//或后
    网址:destination.com
};$ HTTP(配置).success(函数(){
    //成功
})错误(函数(){
    //失败
});

我不能确定的唯一事情是 window.btoa ,如果它是Base64编码的RFC2045-MIME标准的变体,那么你就不错。

但我的例子是过于简单化。从本质上讲,你应该确定服务器支持的认证方案。这可能是由 IANA 下列规定之一:


  • 基本

  • 承载

  • 摘要

  • HOBA

  • 协商

  • 的OAuth

根据所需的方案,应该相应地构成了请求头。

I have this curl command that I would like to simulate with angular:

curl -k -F fieldName=@data.json -u username:Password url

At the moment I went about doing an angular post. However, I run into the problem of authentication. There is no parameter for me to put the user id and password.

Angular code:

   $scope.postCall = function () {
        $scope.ngResult = "clicked";

        var paramsJson = {
            "imessageIdT": $scope.messageIdT,
            "ilobT": $scope.lobT,
            "iregionIdT": $scope.regionIdT,
            "iassetClassT": $scope.assetClassT,
            "additionalInfoT": $scope.additionalInfoT

        };

      var config = {
        paramsJson: paramsJson
      };

      $http.post("WEBSITE", paramsJson, config)
        .success(function (data, status, headers, config)
        {
          $scope.ngResult = logResult("POST SUCCESS", data, status, headers, config);
          //$scope.ngResult = "Yes";
        })
        .error(function (data, status, headers, config)
        {
          $scope.ngResult = logResult("POST ERROR", data, status, headers, config);

          //$scope.ngResult = "No";
        });


    };  

解决方案

Assuming basic authentication, not tested, this might work:

var username = "...", password = "***";
var config = {
    headers: {
        Authorization: "Basic " + window.btoa(username+":"+password)
    },
    method: "get", // or "post",
    url: "destination.com"
};

$http(config).success(function(){
    // on success
}).error(function(){
    // on failure
});

The only thing I'm not certain about is window.btoa, if it's an RFC2045-MIME compliant variant of Base64, then you're good.

But my example is an over-simplification. Essentially, you should determine the authentication scheme supported by the server. It could be any one the following specified by IANA:

  • Basic
  • Bearer
  • Digest
  • HOBA
  • Negotiate
  • OAuth

Depending on the required scheme, you should compose the request header accordingly.

这篇关于如何模拟与角卷曲命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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