0

i'm a bit new in python and working on an API. I'd like to return a URI which accepts my 2 paramaters,which are input (target_group_id, date), this is my base url

get_customer_action_by_target_group_url = \
    'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=&date='

and this is my function.

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

This should pass the paramters in my url which needs to look like this: https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=19&date=20 july 2017 After passing the date and the target groupe_id , but i'm getting this rather https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s7220%20July%202017. How could i fixe this ? any sample of code which could help?? Thank you

holdenweb
  • 33,305
  • 7
  • 57
  • 77
Patcho
  • 77
  • 8
  • 2
    Possible duplicate of [How to percent-encode URL parameters in Python?](https://stackoverflow.com/questions/1695183/how-to-percent-encode-url-parameters-in-python) – agtoever Jul 24 '17 at 07:48
  • 1
    How the send_request function builds the url? Are you using any framewrok to do that? – Martin Alonso Jul 24 '17 at 07:48
  • One more suggestion: try to apply the [SRP](https://en.m.wikipedia.org/wiki/Single_responsibility_principle). This means: don't mix user interaction with other functionality. In your code, if target_group_id is missing and date is not (or the other way around), the used as asked to input both... – agtoever Jul 24 '17 at 07:56

2 Answers2

0

I'd strongly urge you to consider using the requests library ("HTTP for humans"), which was specifically designed to make solving this kind of problem easier than with the standard library alone.

Given

url = 'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup'
tgid = 19
date = '20 July 2017'

you can perform the HTTP request you want with

data = {'TargetGroupID': tgid, 'date': date}
requests.get(url, params=data)

This will do all the tricky stuff correctly and avoid you having to solve problems that have been solved (even for the gnarly corner cases) many times before.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
-1

You can try this:

>>> get_customer_action_by_target_group_url = 'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID={0}&date={1}'
>>> 
>>> targetGroupID=19
>>> date="20 july 2017"
>>> 
>>> get_customer_action_by_target_group_url.format(targetGroupID, date)
'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=19&date=20 july 2017'
JkShaw
  • 1,927
  • 2
  • 13
  • 14