将2个不同的参数添加到ann URL并返回自定义响应

时间:2017-07-21 14:22:36

标签: python json

我还是蟒蛇新手, 我想返回一个接受我的2个参数的URI,它是输入的(target_group_id,date),这是我的基本网址,get_customer_action_by_target_group_url =' https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=&date='这是我的功能。

def get_customer_action_by_target_group(self):
    payload = {"TargetGroupID": "%s" % self.TargetGroupID, "Date":"%s" % self.date,
                }
    if not self.TargetGroupID or not self.date:
        get_target_group_id = (raw_input("Please provide the target Group id:"))
        get_date = (raw_input("Please provide the date as required:"))
        self.TargetGroupID = get_target_group_id
        self.date = get_date
    response = self.send_request(self.get_customer_action_by_target_group_url + self.TargetGroupID +
                                  self.date,
                                 json.dumps(payload), "GET")
    print response, response.text, response.reason
    return response

这应该传递我的网址中的参数,这些参数需要如下所示: https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=19&date=20 2017年7月在通过日期和目标groupe_id之后,我得到的却是https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s7220%20July%202017。我怎么能解决这个问题?任何可以帮助的代码示例?谢谢

1 个答案:

答案 0 :(得分:1)

我假设您的基本字符串为“https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s”,然后您的代码应为

def get_customer_action_by_target_group(self):
    payload = {"TargetGroupID": "%s" % self.TargetGroupID, "Date":"%s" % self.date
            }
    if not self.TargetGroupID or not self.date:
        get_target_group_id = (raw_input("Please provide the target Group id:"))
        get_date = (raw_input("Please provide the date as required:"))
        self.TargetGroupID = get_target_group_id
        self.date = get_date
    response = self.send_request(self.get_customer_action_by_target_group_url % (self.TargetGroupID, self.date)
                             json.dumps(payload), "GET")
    print response, response.text, response.reason
    return response

然后pyton将原始链接中的%s值替换为self.TargetGroupID和self.date中的值

此外,20%20July%202017的部分符合预期,因为在URI中,空间被%20逃脱,因此它表示2017年7月20日。