Postman 在前提脚本中完全构建的请求发送

huangapple go评论57阅读模式
英文:

Postman send request entirely constructed in pre-requisite script

问题

我有以下一段代码,在“Pre-request Script”部分生成多个订单。
我不需要在“Body”部分放任何内容。但是只在请求体中发送 [{}] 会导致400错误。

var guid = (function() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return function() {
        return 'HolCal' + s4() + '-' + s4() + '-' + s4() + '-' +
            s4();
    };
})();
var requestKeyNum = 2;

var orders = [];
for (var i = 0; i < requestKeyNum; i++) {

    var key = guid();
    orders.push({
        "key": key,
        "tradeDate": "2019-07-03",
        "settleDate": "2019-07-04",
        "transactionCode": "B",
        "fundingCurrencySecurity": {
            "secId": 1894823,
            "secType1": "CASH",
            "secType2": "NA",
            "secType3": "NA",
            "secType4": "NA",
            "assetClass": "C",
            "exchangeCode": "",
            "tradeCurrencyCode": "USD",
            "maturityDate": null,
            "mortgageClass": null,
            "investIdType": "D",
            "investId": "9999USD",
            "clearingHouseCode": null,
            "settlementLocation": "PHY",
            "expirationDate": null,
            "issueCountry": ""
        }
    })

}

postman.setEnvironmentVariable("orders", JSON.stringify(orders));
英文:

I have this below piece of code that generates me multiple orders in Pre-request Script section.
I dont need to have anything in "Body" section. But sending just [{}] in the body gives me 400 error.

var guid = (function() {
function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
}
return function() {
    return &#39;HolCal&#39; + s4() + &#39;-&#39; + s4() + &#39;-&#39; + s4() + &#39;-&#39; +
        s4();
};
})();
var requestKeyNum = 2; 

var orders = [];
for (var i = 0; i &lt; requestKeyNum; i++) {

var key = guid();
orders.push({
  &quot;key&quot;: key,
  &quot;tradeDate&quot;: &quot;2019-07-03&quot;,
	&quot;settleDate&quot;: &quot;2019-07-04&quot;,
	&quot;transactionCode&quot;: &quot;B&quot;, 
	&quot;fundingCurrencySecurity&quot; :{
   &quot;secId&quot;:1894823,
		&quot;secType1&quot;: &quot;CASH&quot;, 
		&quot;secType2&quot;:&quot;NA&quot;,
		&quot;secType3&quot;:&quot;NA&quot;, 
		&quot;secType4&quot;:&quot;NA&quot;, 
		&quot;assetClass&quot;:&quot;C&quot;,
		&quot;exchangeCode&quot;:&quot;&quot;,
		&quot;tradeCurrencyCode&quot;:&quot;USD&quot;,
		&quot;maturityDate&quot;:null,
		&quot;mortgageClass&quot;:null, 
		&quot;investIdType&quot;:&quot;D&quot;,
		&quot;investId&quot;:&quot;9999USD&quot;, 
		&quot;clearingHouseCode&quot;:null, 
		&quot;settlementLocation&quot;:&quot;PHY&quot;, 
		&quot;expirationDate&quot;:null, 
		&quot;issueCountry&quot;:&quot;&quot;
	}
	})

}


postman.setEnvironmentVariable(&quot;orders&quot;, JSON.stringify(orders));

答案1

得分: 1

你生成的请求体应该是什么样的?
我假设你想在请求体中发送生成的订单。
如果这是真的,你必须在请求体中引用订单变量。在请求体面板中使用{{orders}}来实现这一点。

英文:

How must your generated body look like?
I assume, that you want so send you generated orders in your body.
If its true, you must reference to the orders variable in your body. Use {{orders}} in your body panel to do this.

huangapple
  • 本文由 发表于 2020年1月4日 00:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定