python - Sending JSON Object in URL -
i'm working diffbot api in python, , have feature can send batch request contains 50 urls in 1 http requests. problem not know how construct such script.
i'm getting stuck @ start, here have.
import requests import json url = 'http://www.diffbot.com/api/' batch = {"method": "get", "relative_url": "/api/article?url=http%3a%2f%2fblogs.wsj.com%2fventurecapital%2f2012%2f05%2f31%2finvestors-back-diffbots-visual-learning-robot-for-web-content%2f%3fmod%3dgoogle_news_blog%26token=xxx"},{"method": "get", "relative_url": "/api/article?url=http%3a%2f%2fgigaom.com%2fcloud%2fsilicon-valley-royalty-pony-up-2m-to-scale-diffbots-visual-learning-robot%2f%26token=xxx"} r = requests.get(u+batch)
now, of course error says str , tuples cannot concatenate, i'm lost how i'd pass json object in form of url.
if point me in right direction appreciated.
here example of how perform call curl, if knows how recreate in python.
i can't seem work. following curl code provided example in documentation. idea of how recreate in python?
curl -d 'token=...' -d 'batch=[ {"method": "get", "relative_url": "/api/article?url=http%3a%2f%2fblogs.wsj.com%2fventurecapital%2f2012%2f05%2f31%2finvestors-back-diffbots-visual-learning-robot-for-web-content%2f%3fmod%3dgoogle_news_blog%26token=..."}, {"method": "get", "relative_url": "/api/article?url=http%3a%2f%2fgigaom.com%2fcloud%2fsilicon-valley-royalty-pony-up-2m-to-scale-diffbots-visual-learning-robot%2f%26token=..."} ]' http://www.diffbot.com/api/batch
you want serialize , base64 encode.
import base64 encoded = base64.urlsafe_b64encode(json.dumps(batch))
now it's safe embed in url.
to object:
json.loads(base64.urlsafe_b64decode(encoded))
Comments
Post a Comment