Response streamed [200 OK]> is not JSON serializable stream image python
<p>I am looking for a way to serve some images from the bing map without sharing the URL (because it contains the secret key) and return it to the client (without saving to disk first).  I'm using this method from <a href="http://flask.pocoo.org/snippets/118/" rel="nofollow">here</a></p>

<pre><code>def get_stream_from_url(url):
    req = requests.get(url, stream = True)
    return Response(stream_with_context(req.iter_content()), content_type = req.headers['content-type'])
</code></pre>

<p>I use it in another method, where I generate a list with those info for those image, so basically I wan to put the  sream response inside the json as a value of key <code>url</code>: </p>

<pre><code>   def images_data_and_url(self, data):
        results = []
        for i in range(1, 13):
            image = {}
            description = data['description']
            url = self.get_stream_from_url(data['url'])
            tile = {"description": description
                    "url": url}
           results.append(image)
        print results
        return json.dumps(results)
</code></pre>

<p>EDIT: 
If I print the results I have this:</p>

<pre><code>&lt;type 'list'&gt;: [{'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;} , {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;},{'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}, {'description': ' this is an example', 'url': &lt;Response streamed [200 OK]&gt;}]
</code></pre>

<p>If I do like this I get this error </p>

<pre><code>&lt;Response streamed [200 OK]&gt; is not JSON serializable
</code></pre>

<p>This because of the function <code>get_stream_from_url</code> returns a response :/ My problem is that I don't know how to send it in another way? </p>

<p>In HTML I get the data and assign per the data for each image like this:</p>

<pre><code>&lt;img class="image1 " data-description="The description" src=""&gt;
</code></pre>

<p>Can somebody help me, how to encode the stream so I can send it as with the JSON data?</p>

<p><code>Flask</code> is overkill for this. I've created a library called <code>wsinfo</code>, which is available here: <a href="https://github.com/linusg/wsinfo" rel="nofollow">https://github.com/linusg/wsinfo</a>.</p>

<p>With it, the code is as simple as:</p>

<pre><code>import base64
import wsinfo
import sys


def html_image(data, content_type):
    if sys.version.startswith("3"):
        from io import BytesIO
        f = BytesIO(data.encode())
    else:
        from StringIO import StringIO
        f = StringIO(data)

    encoded = base64.b64encode(f.read())
    return "data:{};base64,{}".format(content_type, encoded)

url = "http://t0.tiles.virtualearth.net/tiles/a1210223003.jpeg?g=854&amp;mkt=en-US"
w = wsinfo.Info(url)
img = html_image(w.content, w.content_type)

with open("out.html", "w") as html:
    html.write('&lt;img class="image1" data-description="The description" src="{}" /&gt;'.format(img))
</code></pre>

<p>The code runs with both Python 2 and 3, and results in an HTML file called <code>out.html</code>, where the image is embedded.</p>

python json python-2.7 bing-maps bing