0

This function needs to send a JSON dict in python2.7 as a "file" using a post request but the script is running in a container and cannot write to disk.

The endpoint also cannot be changed to accept JSON, rather than a file, as its part of a larger project.

def _json_to_bucket(request_json, thing_id):
    import json
    import requests

    request_url = "https://backend.com/thing/{}".format(thing_id)

    data = {
        'name': 'thing-{}'.format(datetime.isoformat(datetime.now())),
        'url': '/thing_importer/',
        'files': [json.loads(request_json)]
    }
    requests.post(request_url, data=data)

11
  • Welcome. It's not clear what are you trying to do. In your example data['files'] will contain list with single element which is object parsed from json string passed in request_json value. Commented Jun 10, 2020 at 16:51
  • Hello, sorry for making it not clear. if i used files: [open('test.json')] I think it would work. Normally I would save the dict "data" as a json file. Then open it using the example line. edit: hit enter by mistake my typing Commented Jun 10, 2020 at 18:11
  • You can use json.load(f) but how do you want to send this data to server? Commented Jun 10, 2020 at 18:13
  • (made an edit after your reply). So simply put how to I convert a python dict into a python file object without ever writing to a file? I have the dict and need to covert it to a file object that you would normally obtain from doing open('test.json', 'r') Commented Jun 10, 2020 at 18:19
  • Let's start from scratch. Why do you need that? Commented Jun 10, 2020 at 18:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.