1

I've searched high and low for an answer and can't figure out what i'm doing wrong. I'm creating an api that returns json data. I would prefer the response to be printed to the browser as well, if possible. What am I doing wrong?

#!/usr/bin/python

import simplejson as json

class serve_json:
    def __init__(self):
        x = {"msg": "Some message", "error": "Some error"}

        html_to_display = 'Content-Type: application/json\n\n'
        html_to_display += json.dumps(x)
        print html_to_display

serve_json()

The above code doesn't work, and it doesn't print the result to the browser. If I change the Content-Type to "text/html", it prints to the screen fine, but still doesn't work as json data.

  • I'm not using any framework, everything i'm doing is straight python/javascript.
  • This script is executed by an http POST request.
  • This script is executed via /cgi

  • http://grouped.com/cgi-bin/upload_example.php (Works perfectly)

  • http://grouped.com/cgi-bin/upload_example.py (Does not work - Content-Type = text/html)
  • The code listed above also does not work either and represents an identical example as the one above except the Content-Type is set to application/json
8
  • 1
    How are you calling this piece of code from a browser? Via CGI? Apache? You're really, really better off using a framework for this sort of thing. Commented Nov 8, 2011 at 1:07
  • Thanks for the suggestion, Scott. I should have been more specific about that and I apologize. This script gets executed via CGI, as a pearl or php script would. It is executed as the result of an http POST request. Commented Nov 8, 2011 at 1:19
  • Have you been able to send regular HTML via this mechanism? In other words, do you know for sure your CGI environment is set up properly? Commented Nov 8, 2011 at 1:21
  • Oh absolutely. We built our own framework and everything works as expected, except this new piece. Here is a live example: grouped.com/cgi-bin/ajax_picture_upload Commented Nov 8, 2011 at 1:22
  • It might be getting to the browser but not displaying because it's JSON. Have you inspected the response using something like FireBug? Commented Nov 8, 2011 at 1:49

1 Answer 1

3

I'd recommend bottle, it's really easy to build simple little JSON services with it:

from bottle import *

@get('/')
def serve_json():
    return {"msg": "Some message", "error": "Some error"}

run(host='localhost', port=8080)

One neat feature of bottle is that it'll automatically serve JSON from a route that returns a dict. You can execute python serve_json.py to run your app using the built-in HTTP server, host it as a WSGI application, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

Hey Zeekay, thanks for the suggestion. I'm a little confused though as this doesn't seem like what i'm looking for. Since i'm calling this script as a result of a post form request, I don't have the control I would if I were running it internally. If I were using php, I could get away with just printing the content, like this: <?php echo "{"; echo "error: 'Some error',\n"; echo "msg: 'Some message'\n"; echo "}"; ?> But with Python, for some reason it doesn't work the same. I'm having trouble understanding why.
Holy S that was messy. Working php example: grouped.com/cgi-bin/upload_example.php
If you want to use this as a cgi script you could add from wsgiref.handlers import CGIHandler; CGIHandler().run(default_app()). Adding bottle as a dependency isn't a big deal, it's only a single python module.
There are a lot of different ways to deploy Python code, CGI is not ideal. These are the relevant docs though: docs.python.org/library/cgi.html.
Zeekay - Thanks for pointing me to the python cgi documentation. I know it well and again, I think I'm either not explaining myself properly or you aren't understanding the question. I'm happy to assume the former. I have no problem executing python code and have no problem interacting with cgi scripts. I am unable to serve (very specifically) json data back from a post form request. I can do this in php very simply by posting raw text to the browser and evaluating the data. I'm not looking to run additional servers on top of my server to serve json data, just properly present json via Python.
|

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.