4

I will write a python script that listen to a webhook for a custom tool that will send json(They might support other format also) on a port I can specify.

How to write something similar to linux command: "nc -l 9000" to dump the out put I get on that port (header and body)?

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    print()  <- how to get the data here?
    return {"message": "ok"}

I want to print the content in the terminal, then I can easy see what I will get and take action on it. Not sure what I should replay to them if that is even needed (need to check this, they are not done with their part yet).

olle.holm
  • 233
  • 1
  • 4
  • 11
  • If you mean how to request an HTTP(S) page, then you may be looking for could be https://requests.readthedocs.io/en/master/ or https://github.com/encode/httpx – lsabi Sep 04 '20 at 09:10
  • No the other way around I want to dump what comes into the port. The web browser is just one example. How I should like to be able dump other request for other protocol. – olle.holm Sep 04 '20 at 15:49
  • Other than HTTP(S)? I don't think it's possible since uvicorn is an HTTP server. Probably some proxy in front of it can do this kind of job. Otherwise I don't know how to help – lsabi Sep 04 '20 at 16:28
  • Ok, but it´s possible to dump the raw http(s) request? – olle.holm Sep 04 '20 at 17:05
  • 1
    You can handle the request object with a middleware, see https://fastapi.tiangolo.com/tutorial/middleware/ https://fastapi.tiangolo.com/advanced/middleware/ and https://www.starlette.io/middleware/ . Though, the raw request is quite difficult to get with python. Maybe there is a library that can take the request object and transform it, but I've never heard of it – lsabi Sep 04 '20 at 19:59
  • Hey @olle.holm can you update your question with actually what you are trying to do, i 'd be happy to help. – Yagiz Degirmenci Sep 04 '20 at 21:27
  • @YagizcanDegirmenci I have updated it now, hope its more clear. They are not yet done with there part, so just want to start to prepare so Im ready when I get it. – olle.holm Sep 04 '20 at 21:48
  • @olle.holm yup, it's clear enough now, check my answer. – Yagiz Degirmenci Sep 04 '20 at 21:59

1 Answers1

4

So i think you are looking for Request.

This contains a lot of information (including, Body, Form, Headers etc.) about the request.

from fastapi import FastAPI, Request

app = FastAPI()


@app.get("/")
def read_root(request: Request):
    print(request.headers)
    return {}

So now if i send a request to this endpoint this 'll return me

{
    "host":"127.0.0.1:8000",
    "connection":"keep-alive",
    "accept":"application/json",
    "sec-fetch-site":"same-origin",
    "sec-fetch-mode":"cors",
    "sec-fetch-dest":"empty",
    "referer":"http://127.0.0.1:8000/docs",
    "accept-encoding":"gzip, deflate, br",
    "accept-language":"en-US,en;q=0.9,tr;q=0.8",
    "cookie":"csrftoken=sdf6ty78uewfıfehq7y8fuq; _ga=GA.1.11242141,1234423"
}

Then you can extract the accept from request.headers

If you only need that, you can also do

@app.get("/")
def read_root(request: Request):
    print(request.headers['accept'])
    return {}

This will return only

Out: application/json
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85