Archive
Flask RESTful POST JSON
Problem
Using Flask-RESTful, I needed an API endpoint that accepts JSON data.
Solution
I found the solution here: http://stackoverflow.com/questions/22273671/flask-restful-post-json-fails. You can copy / paste that code. Note that the JSON data is POSTed to your API endpoint, thus you need to implement the post() method.
However, how to test it?
1) using cURL:
$ curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d "{\"Hello\":\"Karl\"}" http://domain/your_api_endpoint
Damn, that’s compicated, right? Is there an easier way?
2) using httpie:
You can install httpie with your favorite package manager. Then:
$ http POST http://domain/your_api_endpoint Hello=Karl
get the tweets of a user and save them in CSV
remove tags from HTML
Problem
You have an HTML string and you want to remove all the tags from it.
Solution
Install the package “bleach” via pip. Then:
>>> import bleach
>>> html = "Her <h1>name</h1> was <i>Jane</i>."
>>> cleaned = bleach.clean(html, tags=[], attributes={}, styles=[], strip=True)
>>> html
'Her <h1>name</h1> was <i>Jane</i>.'
>>> cleaned
'Her name was Jane.'
Tip from here.
