Question

I am running into an issue that I can't seem to get past. Any insight would be great.

The script is supposed to get memory allocation information from a database, and return that information as a formatted JSON object. The script works fine when I give it a static JSON object will stack_ids (the information I would be passing) but it won't work when I try to pass the information via POST.

Although the current state of my code uses request.json("") to access the passed data, I have also tried request.POST.get("").

My HTML includes this post request, using D3's xhr post:

var stacks = [230323, 201100, 201108, 229390, 201106, 201114];
var stack_ids = {'stack_ids': stacks};
var my_request = d3.xhr('/pie_graph');
my_request.header("Content-Type", "application/json")
my_request.post(stack_ids, function(stuff){ 

stuff = JSON.parse(stuff);
var data1 = stuff['allocations'];
var data2 = stuff['allocated bytes'];
var data3 = stuff['frees'];
var data4 = stuff['freed bytes'];
...
...
}, "json");

while my server script has this route:

@views.webapp.route('/pie_graph', method='POST')
def server_pie_graph_json():
    db = views.db
    config = views.config
    ret = {
        'allocations' :     [],
        'allocated bytes' : [],
        'frees' :           [],
        'freed bytes' :     [],
        'leaks' :           [],
        'leaked bytes' :    []
    }

    stack_ids = request.json['stack_ids']

    #for each unique stack trace
    for pos, stack_id in stack_ids:
        stack = db.stacks[stack_id]

        nallocs = format(stack.nallocs(db, config))
        nalloc_bytes = format(stack.nalloc_bytes(db, config))
        nfrees = format(stack.nfrees(db, config))
        nfree_bytes = format(stack.nfree_bytes(db, config))
        nleaks = format(stack.nallocs(db, config) - stack.nfrees(db, config))
        nleaked_bytes = format(stack.nalloc_bytes(db, config) - stack.nfree_bytes(db, config))

        # create a dictionary representing the stack
        ret['allocations'].append({'label' : stack_id, 'value' : nallocs})
        ret['allocated bytes'].append({'label' : stack_id, 'value' : nalloc_bytes}) 
        ret['frees'].append({'label' : stack_id, 'value' : nfrees})
        ret['freed bytes'].append({'label' : stack_id, 'value' : nfree_bytes})
        ret['leaks'].append({'label' : stack_id, 'value' : nleaks})
        ret['leaked bytes'].append({'label' : stack_id, 'value' : nfree_bytes})


    # return dictionary of allocation information
    return ret

Most of that can be ignored, the script works when I give it a static JSON object full of data.

The request currently returns a 500 Internal Server Error: JSONDecodeError('Expecting value: line 1 column 2 (char 1)',).

Can anyone explain to me what I am doing wrong?

Also, if you need me to explain anything further, or include any other information, I am happy to do that. My brain is slightly fried after working on this for so long, so I may have missed something.

Was it helpful?

Solution

Here is what I do with POST and it works:

from bottle import *
@post('/')
def do_something():
    comment = request.forms.get('comment')
    sourcecode = request.forms.get('sourceCode')

Source

function saveTheSourceCodeToServer(comment) {
  var path = saveLocation();
  var params = { 'sourceCode' : getTheSourceCode() , 'comment' : comment};
  post_to_url(path, params, 'post');
}

Source with credits to JavaScript post request like a form submit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top