简体   繁体   English

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

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

I want to hash a dictionary in python 2.7 to obtain the same result as I get in postman by calling pm.request body.我想 hash 字典 python 2.7 以获得与我在 postman 中通过调用 pm.request 正文获得的结果相同的结果。 The hashing method works correctly when I try with strings, but I can't get the body to hash the same.当我尝试使用字符串时,哈希方法可以正常工作,但我无法将正文与 hash 相同。 I'm using OrderedDictionary from collections package in python.我在 python 中使用来自 collections package 的 OrderedDictionary。 I assumed that the pm.body.request would be a JSON but it does not seem to be the case.我假设 pm.body.request 将是 JSON 但似乎并非如此。

pm-body下午身体

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

Pre-request script in PM 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");

What I've tried in Python我在 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")

        

This is the expected value for hashInBase64 after running the Pre-request script '25iZC8NycCILZJCGN9T2jachFANGD4HkLSp+8X0W/Jk='这是运行预请求脚本 '25iZC8NycCILZJCGN9T2jachFANGD4HkLSp+8X0W/Jk=' 后 hashInBase64 的预期值

Solution for your problem:解决您的问题:

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

The correct approach:正确的做法:

You should remove spaces to make it work across all applications:您应该删除空格以使其适用于所有应用程序:

pm.request.body doesn't return just the body content but the full body object with the content type pm.request.body 不仅返回正文内容,还返回带有内容类型的完整正文 object

so you should use所以你应该使用

pm.request.body.raw

But still this will have space in it, you can remove it using但这仍然会有空间,您可以使用删除它

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

Now the same space issue is there in the python code you can remove it using现在 python 代码中存在相同的空间问题,您可以使用

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

Python code: 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 pre-request: 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.

相关问题 如何访问存储从 postman 发送到 Django 服务器的文件数组的发布请求正文的字段? - How to access a field of the body of a post request that stores an array of files sent from postman to a Django server? 将 POST 请求从邮递员导出到 Python - Export POST Request from postman to Python 在chrome上获取请求的正文,并在邮递员请求中使用 - Fetch the body of a request on chrome and use it on a postman request Postman API 请求在 Python - Postman API request in Python 如何在使用来自 AWS Lambda 的 307 重定向(POST 方法和正文)在 python 中传递请求正文 - How to do pass a Request Body while doing a 307 Redirect with a( POST Method and body ) from AWS Lambda , in python 使用python时来自POSTMAN的GET请求失败 - GET request working from POSTMAN failed while using python 如何使用python CGI解析“请求体”? - How to parse the “request body” using python CGI? 如何在 urllib3 python 中打印请求正文 - how to print request body in urllib3 python 如何将标头和正文添加到 python 请求 - How to add headers and body to a python request Postman 预请求脚本从 CSV 文件读取变量并附加到 JSON 正文 - Postman Pre-Request Script to Read Variables from CSV file and Append to JSON Body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM