3

I am editing the tags of a question via the API, and to do that you have to pass the body and title etc.

I can get the Markdown or HTML from the API but I can't see how to send it back formatted properly.

These revisions pretty much show what I've tried:

So how do I edit it neatly?


Even direct copy / paste from the source page into the body box gives bad results.

  • 1
    Sequence matters. It should be \r\n and the string should have the hex bytes 0x0D, 0x0A (before form encoding). Make sure you escape (or don't) the `\`s properly for whatever language you are using. – Brock Adams May 27 '15 at 11:10
  • @BrockAdams so i should be giving \\\r\\\\n instead? – Tim May 27 '15 at 11:16
  • The form encoded string that your app sends should contain %0D%0A bits. If it has %250D%250A, you're miss/over encoding. What language are you using? For most languages, it's just \r\n in a plain ASCII string. – Brock Adams May 27 '15 at 11:16
  • @BrockAdams Python. The API is saying I'm calling it wrong now, but when I use the body box on the api page, it gives messy code: askubuntu.com/revisions/374644/22 – Tim May 27 '15 at 11:35
  • 1
    @BrockAdams thanks :) Worked it out now - I was using a get request. \r\n works perfectly! – Tim May 27 '15 at 12:00
  • 1
    btw @Tim you could use The API Sandbox for editing... Or the Formatting Sandbox :) – ᔕᖺᘎᕊ May 27 '15 at 15:13
  • @ᔕᖺᘎᕊ I did consider it, but I wanted to keep it getting the questions from the search, so it had to be one that came up there. Will do in the future! – Tim May 27 '15 at 15:14
  • @Tim no problem :) just a suggestion! (it's good the load-of-edits community-wiki conversion has gone now! :) – ᔕᖺᘎᕊ May 27 '15 at 15:15
  • 2
    You should post an answer here (or delete this Q). Include an example of the Python code you ended up using. – Brock Adams May 27 '15 at 20:26
4

The \r\n issue was solved when I changed to a POST request.

But, since I was retagging questions (didn't really need to change the body, just resend it), I ended up using the body_markdown, encoded with HTMLParser.HTMLParser().unescape()

body_markdown = str(HTMLParser.HTMLParser().unescape(question_data['items'][0]['body_markdown']))

And then sending it with:

response = requests.post('https://api.stackexchange.com/2.2/questions/'+str(q_id)+'/edit',
      data={'body': body_markdown,
            'comment': 'removed '+tag+' tag',
            'tags': ' '.join(tags),
            'title': title,
            'access_token': token,
            'site': site,
            'key': key
            }
        )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .