Archive
Archive for July, 2016
Flask RESTful POST JSON
July 17, 2016
Leave a comment
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
Categories: flask, python
curl, Flask-RESTful, httpie, post request, REST API
get the tweets of a user and save them in CSV
July 16, 2016
Leave a comment
remove tags from HTML
July 13, 2016
Leave a comment
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.
