python 字典转json去除多余空格符

python 字典转json去除多余空格符

最近在测一个接口时,遇到这么个情况,用jmeter去请求可以请求到正确的结果。但用python request 去请求时,入参都是正确的,但后端老是识别不了正确的内容。后面查看后端的日志,入参的信息是正确,但是python requests请求的json格式入参中,在冒号:后面自动带有空格符导致后端接口返回的结果对不上,如:{'city':  '深圳', 'addr':  '南山区科技园xx号xx楼', 'name':  'jctang', 'tel':  '151xxxxxxxx'}

后面将python 脚本改为再入参前将字典转为json并将自带的空格符去掉,返回的结果才是正确。#encoding:utf-8

import requests
import json


class Recipient_api(object):
    def __init__(self,reUrl,params,header):
        self.baseUrl = reUrl
        self.params = params
        self.header = header
    
    def request_Recipient(self):
        with requests.post(self.baseUrl,self.params,headers=self.header,verify=False) as response:
            return response.text




if __name__=="__main__":
    baseUrl = "http://www.xxx.xx.com/v6/xxx/xxx"
    re_params = {
            "city": "深圳",
            "addr": "南山区南头xx号xx楼",
            "name": "jctang",
            "tel": "151xxxxxxxx",
        }
    re_header = {
        "cookies":"BD231RCV3FR7xVh6AAEQ6RIgPGIA7EQhP8; path=/;",
        "content-type":"application/json",
        
    }
    params_json = json.dumps(re_params, separators=(',', ':'), ensure_ascii=False)
    debug_value = Recipient_api(reUrl=baseUrl,params=re_params,header=re_header).request_Recipient()
    print(debug_value)

  

 

posted @ 2022-07-11 00:40  西夏一品唐  阅读(1050)  评论(0编辑  收藏  举报