有没有办法在rabbitmq basic_publish函数中将json作为参数传递?

时间:2021-07-16 07:40:49

标签: python json rabbitmq mailgun fastapi

我正在尝试发送 json 数据,其中我使用 fstring 使数据动态化。当将它传递给 basic_publish 时,我得到 raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable 我已经检查了其他相关问题,但我似乎无法解决它,我阅读了 this question

这是我的代码


    # confuguring sender
    def connect_queue():                                                                     
        rabbitmq = pika.BlockingConnection(                                                         
            pika.ConnectionParameters("localhost")                                    
        )                                                                                             
        return rabbitmq
    
    def mail_pwd_sender():  
                                                                  
        conn = connect_queue()                                                                        
        channel = conn.channel()                                                                      
        channel.queue_declare(queue='mail_pwd_queue', durable=True)                                    
        channel.queue_bind(exchange='amq.direct', 
        queue='mail_pwd_queue')                              
        mail_pwd_queue = channel                                                                     
        return mail_pwd_queue

   

        data = {
            
          "url":f"https://api.mailgun.net/v3/{Domain_name}/messages",
          "auth" : ("api", f"{API_KEY}"),
          "from_" : {"from": "mailgun@xxxxxxxxx.mailgun.org"},
          "data" : {"to": [recipient_email],
                "subject": subject,
                "html" : html,
                "recipient-variables":json.dumps(
                {f{recipient_email}": {"password": f"{body}" }})}
                    }
   
        send_queue = mail_pwd_sender()
        send_queue.basic_publish(                                                        
            exchange='amq.direct',                                              
            routing_key='mail_pwd_queue',                                        
            body=json.dumps(data),                                                         
            properties=pika.BasicProperties(                                    
                delivery_mode=2                         
            )                                                                   
        )  

0 个答案:

没有答案