简体   繁体   中英

Formatting GET request from Eve interface

I am using Eve as the REST interface on my mongodb instance. I would like to save the output of a particular field only, rather than the entire payload.

Right now, for the following command:

curl -g http://xxx.xxx.xxx.xx:xxxx/people?where={%22first_name%22:%20%22myself%22} -o "C:\\Users\xxx\Desktop\output.txt"

I save the output to output.txt file, and it looks like this:

{"_items": [{"_id": "5a5f753e24b8bd18d4d28593", "file": "BASE64STRING", _updated": "Wed, 17 Jan 2018 16:09:34 GMT", "_created": "Wed, 17 Jan 2018 16:09:34 GMT", "_etag": "f38ef69eda077456da63ce8246a1d6665413f1cb"}]}

Where BASE64 string is the image I retrieved from the database. How can I save only the BASE64 string rather than saving the entire "items,id,file" etc from the GET request?

You can use urllib (or urllib2 for Python2) to get the response; urllib is python's default library for accessing internet resources.
Then you can process the response content with json , select the "file" item, and save to file.

import urllib.request 
import json

url = 'http://xxx.xxx.xxx.xx:xxxx/people?where={"first_name": "myself"}'
r = urllib.request.urlopen(url)
j = json.loads(r.read().decode())
data = j['_items'][0]['file']

with open('C:\\Users\xxx\Desktop\output.txt', 'w') as f:
    f.write(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM