F
F
fantom00052016-05-10 10:17:22
Python
fantom0005, 2016-05-10 10:17:22

How to pass the request to another URL?

Hello. I am using Tornado server. The bottom line is that the front sent a request to a third-party service at a given URl. Now I need to implement the Handler to which this request is sent, and this handler must send the request to the desired url. Those. I need to implement an intermediary so that the front sends requests only to my server, and the server is already on the necessary services. The bottom line is that you need to transfer all headers, cookies and the request body to the given url.
How can I implement this?
Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladimir Kozlovsky, 2016-05-15
@vladkozlovsky

An example of how this can be done on aiohttp/python3.5:

from aiohttp import web, ClientSession

class ProxyHandler(web.View):

    BAD_REQUEST_HEADERS = ['host', 'accept-encoding']
    BAD_RESPONSE_HEADERS = ['content-encoding', 'transfer-encoding']

    async def get(self):
        url = request.GET.get('url')

        request_headers = []
        # убиваем "вредные" залоговки запроса
        for key, value in request.headers.items():
            if key.lower() in self.BAD_REQUEST_HEADERS:
                continue
            response_headers.append(key, value)

        with ClientSession() as session:
            async with session.get(url, headers=request_headers) as response:

                response_headers = []
                # убиваем "вредные" залоговки ответа
                for item in response.headers:
                    key, value = item
                    if key.lower() in self.BAD_RESPONSE_HEADERS:
                        continue

                    response_headers.append((key, value))

                body = await response.read()
                return web.Response(body=body, headers=response_headers, status=response.status)

I wrote from memory and did not check, there may be typos, but the logic is this.

D
Dmitry, 2016-05-10
@dmtrrr

What's the problem? Use the requests library.

A
Alexey, 2016-05-10
@MAKAPOH

According to the documentation, you need an AsyncHTTPClient . The required parameters can be set via the Request object passed to the fetch method . Try it.

S
Stanislav Pugachev, 2016-05-10
@Stqs

looks like you need a reverse proxy (for example https://www.nginx.com/resources/admin-guide/revers...

A
Alexey Ukolov, 2015-06-25
@heksen

You return an array of associative arrays, which in js turns into an array of objects. And this array does not, of course, have an id property . Try to output to the console obj to the console after parsing and see for yourself.
Well, your request can be replaced by

$.getJSON(
    "db.php",
    { func: "GetFullInfo", id: CurId },	     	   
    success: function( data )		
    {
        // здесь data уже распарсенный json
    }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question