python %s is discarding \n while sending via POST method

I am working on an email sending project in python

when i pass %s in post method with a variable its discarding all new lines and sending it to email so im getting all stuffed emails can somebody please help

post method :

r = requests.post('https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1=%s' % (s))

so if s contains a news with headline and body, in email im getting it all concatenated together

2 answers

  • answered 2017-11-12 19:46 CHURLZ

    If on python3, try 'value1={}'.format(s) Or if on 3.6, try f'value1={s}'

  • answered 2017-11-12 19:55 alexisdevarennes

    Use format() to construct that url. Also, learn about using named placeholders using format(), the example provided below makes use of them.

    s = s.replace('\n', '%0A')  # Replaces \n with urlencoded \n (%0A)
    url = 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1={value1}'.format(value1=s)
    r = requests.post(url)
    

    You could also use urlencode.

    >>> import urllib
    >>> f = { 'value1' : s}
    >>> urllib.urlencode(f)
    'value1=cool+event'