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

2024-06-28 15:14:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python2.7中对字典进行散列,通过调用pm.request body获得与在postman中相同的结果。当我尝试使用字符串时,哈希方法可以正常工作,但是我无法让主体对相同的字符串进行哈希。我正在使用python中的OrderedDictionary from collections包。我假设pm.body.request将是一个JSON,但事实似乎并非如此

pm机构

{
  "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的预期值


Tags: 字符串requestvarbodyhasharodpm
1条回答
网友
1楼 · 发布于 2024-06-28 15:14:24

问题的解决方案:

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")

邮递员:

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不仅返回body内容,还返回内容类型为的完整body对象

所以你应该使用

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")

邮差预先请求:

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");

相关问题 更多 >