简体   繁体   中英

Why can't I decode a JSON message in the following situation?

I am trying to send a JSON message from a computer to another one via a post request. The script which sends the message is the following:

message = {'station':'turn on'}
res = rest.send( 'POST', server_addr + "/newstation", json.dumps(message), {'Content-Type': 'application/json'} )

The rest.send(...) method should be correct as I used it before and it worked fine.

The PC which sends the post request runs Linux, while the receiving one runs Win 8, if that means anything.

On the receiving machine I have the following:

@app.route('/newstation', methods = ['POST'])
def new_station ():
    j_data = request.get_json()
    d = decode_data(j_data)

where decode_data(j_data) is the following

def decode_data(j_data):
    d = json.loads(j_data)
    return d

My problem is: whenever I try to send the post request from the first machine the response is "Internal server error" and on the machine with the server the error returned is "TypeError: expected string or buffer". Now I am thinking that it may be a matter of encoding of the string.

The post request is received and I can print the json content without problems, the issue arises when I try to decode.

I fixed the issue, it was a mistake on my part (of course). I misunderstood the documentation.

@app.route('/newstation', methods = ['POST'])
def new_station ():
   j_data = request.get_json()
   #d = decode_data(j_data)

request.get_json() already returns me a dictionary, so the decode_data function isn't actually needed. I already have the result without the need for json.loads().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM