[Django]-Python dictionary comes with an extra curly bracket, as a JSON key, through HttpResponse

2👍

The way you sent your data is sort of wrong.

When you are using GET, the url is like:

http://127.0.0.1:8000/?key1=value1&key2=value2

What you did above actually makes the json string the key instead of value. Hence the wrong request format.

I think what you need to do is like:

url = 'http://127.0.0.1:8000/calc/?payload=' + string

This will make payload the key and your actual string the value. And to retrieve it:

data = request.GET
json_string = data['payload']
# load the string
👤ljk321

2👍

Check this article out.

Specifically,

return HttpResponse(res, content_type = "application/json")

You’re missing the content_type qualifier.

Furthermore, you really should be using serializers.serialize like

def calc(request):
  data = request.GET
  return HttpResponse(serializers.serialize("json", data), 
    content_type = 'application/json')
👤Jason

Leave a comment