0

I recieve a POSTed JSON with mod_wsgi on Apache. I have to forward the JSON to some API (using POST), take API's response and respond back to where the initial POST came from.
Here goes the python code

import requests
import urllib.parse

def application(environ, start_response):
    url = "http://texchange.nowtaxi.ru/api/secret_api_key/"
    query = environ['QUERY_STRING']

    if query == "get":
        url += "tariff/list"
        r = requests.get(url)
        response_headers = [('Content-type', 'application/json')]
    else:
        url += "order/put"
        input_len = int(environ.get('CONTENT_LENGTH', '0'))
        data = environ['wsgi.input'].read(input_len)
        decoded = data.decode('utf-8')
        unquoted = urllib.parse.unquote(decoded)
        print(decoded)  # 'from%5Baddress%5D=%D0%'
        print(unquoted) # 'from[address]=\xd0\xa0'
        r = requests.post(url,data)
        output_len = sum(len(line) for line in r.text)
        response_headers = [('Content-type', 'application/json'),
                            ('Content-Length', str(output_len))]

    status = "200 OK"

    start_response(status, response_headers)

    return [r.text.encode('utf-8')]

The actual JSON starts "{"from":{"address":"Россия
I thought those \x's are called escaped symbols, so I tried ast.literal_eval and codecs.getdecoder("unicode_escape"), but it didn't help. I can't properly google the case, because I feel like I misunderstood wtf is happening here. Maybe I have to somehow change the $.post() call in the .js file that sends POST to the wsgi script?

UPD: my bro said that it's totally unclear what I need. I'll clarify. I need to get the string that represents the recieved JSON in it's initial form. With cyrillic letters, "s, {}s, etc. What I DO get after decoding recieved byte-sequence is 'from%5Baddress%5D=%D0%'. If I unquote it, it converts into 'from[address]=\xd0\xa0', but that's still not what I want

mekkanizer
  • 722
  • 2
  • 9
  • 28
  • read the second part of the [answer that discusses the difference between a string in memory, its text representation in Python source code and why you need to *double escape* some characters to represent a json text literally inside a Python string *literal*](http://stackoverflow.com/a/32539609/4279) – jfs Oct 14 '15 at 15:35
  • @j-f-sebastian So if *any* character can be escaped, I just have to insert escaping symbol ('/', right?) before each %xx in the decoded string, instead of unquotting it? – mekkanizer Oct 14 '15 at 19:35
  • no. It is not clear from your question what do you expect to get here: you should probably fix your client to send json text directly. If you don't know how to make a json request using javascript; ask a separate question (or more likely: find an existing question). – jfs Oct 14 '15 at 19:46
  • j-f-sebastian Pfft man, who needs easy ways :) If those portions_of_alphanumerical_chars after % satisfy Fano condition, I'll write a ~100 lettered dict :) – mekkanizer Oct 14 '15 at 21:00

0 Answers0