Paolo
Paolo

Reputation: 26084

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?

Upvotes: 0

Views: 88

Answers (1)

t.m.adam
t.m.adam

Reputation: 15376

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)

Upvotes: 1

Related Questions