簡體   English   中英

如何在 python 中表示來自 postman 的 pm.request.body

[英]How to represent pm.request.body from postman in python

我想 hash 字典 python 2.7 以獲得與我在 postman 中通過調用 pm.request 正文獲得的結果相同的結果。 當我嘗試使用字符串時,哈希方法可以正常工作,但我無法將正文與 hash 相同。 我在 python 中使用來自 collections package 的 OrderedDictionary。 我假設 pm.body.request 將是 JSON 但似乎並非如此。

下午身體

{
  "personNumber": "195012161930",
  "requestDescription": "Detta ar ett test",
  "verksamhetId": "1ac12a80-819a-42ca-bd51-97bd3e19c443",
  "stadsDelID": "f55a5bea-2398-11e9-8140-0e9ccdb68c09"
}

PM 中的預請求腳本

var hmacKey = 'c811f8ae-dd9f-4b15-9a09-97a09bdbb485'; 

var contentToHash = pm.request.body.raw;
var cryptoJs = require('crypto-js'); 
var hash = cryptoJs.HmacSHA256(contentToHash, hmacKey); 
var hashInBase64 = cryptoJs.enc.Base64.stringify(hash); 
  
console.log(hashInBase64 + " This is the result");

我在 Python 中嘗試過的

        key = "c811f8ae-dd9f-4b15-9a09-97a09bdbb485"

        od = OrderedDict() 
        od['personNumber'] = "195012161111"
        od['requestDescription'] = "Detta ar ett test"
        od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
        od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"

        contentToHash = (json.dumps((od)))

        hash = hmac.new( key, (contentToHash), hashlib.sha256)
        
        baseHash = base64.b64encode(hash.digest())
        print(baseHash + " This is the result in Python")

        

這是運行預請求腳本 '25iZC8NycCILZJCGN9T2jachFANGD4HkLSp+8X0W/Jk=' 后 hashInBase64 的預期值

解決您的問題:

Python2:

from collections import OrderedDict
import json
import hmac
import hashlib
import base64

key = bytearray("c811f8ae-dd9f-4b15-9a09-97a09bdbb485","utf-8")

od = OrderedDict() 
od['personNumber'] = "195012161930"
od['requestDescription'] = "Detta ar ett test"
od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"

contentToHash = json.dumps(od,indent=2,separators=(',', ': '))

contentToHash = contentToHash.replace("\n","\r\n")

print(repr(contentToHash))

hash = hmac.new( key, (contentToHash).encode('utf-8'), hashlib.sha256)

baseHash = base64.b64encode(hash.digest())
print(str(baseHash) + " This is the result in Python")

Python3:

from collections import OrderedDict
import json
import hmac
import hashlib
import base64

key = bytearray("c811f8ae-dd9f-4b15-9a09-97a09bdbb485","utf-8")

od = OrderedDict() 
od['personNumber'] = "195012161930"
od['requestDescription'] = "Detta ar ett test"
od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"

contentToHash = json.dumps(od,indent="\r  ",separators=(',', ': '))

contentToHash=contentToHash.replace("\n\r","\r\n")
contentToHash = contentToHash.replace("\n}","\r\n}")

print(repr(contentToHash))

hash = hmac.new( key, (contentToHash).encode('utf-8'), hashlib.sha256)

baseHash = base64.b64encode(hash.digest())
print(str(baseHash) + " This is the result in Python")

Postman:

var hmacKey = 'c811f8ae-dd9f-4b15-9a09-97a09bdbb485'; 

var contentToHash = pm.request.body.raw;
console.log(JSON.stringify(contentToHash))
var cryptoJs = require('crypto-js'); 
var hash = cryptoJs.HmacSHA256(contentToHash, hmacKey); 
var hashInBase64 = cryptoJs.enc.Base64.stringify(hash); 
  
console.log(hashInBase64 + " This is the result"); 

正確的做法:

您應該刪除空格以使其適用於所有應用程序:

pm.request.body 不僅返回正文內容,還返回帶有內容類型的完整正文 object

所以你應該使用

pm.request.body.raw

但這仍然會有空間,您可以使用刪除它

JSON.stringify(JSON.parse(pm.request.body.raw))

現在 python 代碼中存在相同的空間問題,您可以使用

contentToHash = json.dumps(od,separators=(',', ':'))

Python代碼:

from collections import OrderedDict
import json
import hmac
import hashlib
import base64

key = bytearray("c811f8ae-dd9f-4b15-9a09-97a09bdbb485","utf-8")

od = OrderedDict() 
od['personNumber'] = "195012161111"
od['requestDescription'] = "Detta ar ett test"
od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"

contentToHash = json.dumps(od,separators=(',', ':'))

hash = hmac.new( key, (contentToHash).encode('utf-8'), hashlib.sha256)

baseHash = base64.b64encode(hash.digest())
print(str(baseHash) + " This is the result in Python")

Postman 預請求:

var hmacKey = 'c811f8ae-dd9f-4b15-9a09-97a09bdbb485'; 

var contentToHash = JSON.stringify(JSON.parse(pm.request.body.raw),null,null)

var cryptoJs = require('crypto-js'); 
var hash = cryptoJs.HmacSHA256(contentToHash, hmacKey); 
console.log(hash)
var hashInBase64 = cryptoJs.enc.Base64.stringify(hash); 
  
console.log(hashInBase64 + " This is the result");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM