Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
Response content shorter than Content-Length

I'm doing a FastAPI app, with a function that authenticates to a CouchDB instance. In order to request Couchdb, I use the (yet unmaintained) library python-couchdb.

Here is the relevant portion of code that illustrates my issue:

from fastapi import FastAPI
from couchdb import http
resource = http.Resource(url, http.Session())
from couchdb import Unauthorized
from pydantic import BaseModel

app = FastAPI()

class RegisteredUser(BaseModel):
    email: str
    password: str

@app.post("/login")
async def log_user(user: RegisteredUser):

    # some email format verifications here
    # ...

    try:

        status, headers, _ = resource.post_json('_session', {
            'name': user.email,
            'password': user.password,
        })

    except Exception as e:
        if isinstance(e, Unauthorized):
            return 403
        else:
            return 500

    # tests
    print(headers)

The headers look like:

Cache-Control: must-revalidate
Content-Length: 54
Content-Type: application/json
Date: Sat, 08 Aug 2020 19:19:49 GMT
Server: CouchDB/3.1.0 (Erlang OTP/22)
Set-Cookie: AuthSession=am9zZWJvdmVAam9zZWJvdmUuY29tOjVGMkVGQUQ2Op-UUD22VvdxYzbMNp92e30Er_z0; Version=1; Expires=Sat, 08-Aug-2020 20:59:50 GMT; Max-Age=6000;     Path=/; HttpOnly

At this point (if no error raised), I'd like to send back to the (browser) client the cookie that CouchDb provides. Something like:

    ...

    # tests
    print(headers)

    if status == 200 and 'Set-Cookie' in headers:
        return JSONResponse(content=True, headers=headers)
    else:
        return status
  1. I'm not used to sessions-cookie and I'm not sure if I should send back the full headers or just the headers['Set-Cookie'] part

  2. Whatever I do, I end up with the same error message

RuntimeError: Response content shorter than Content-Length

Would you mine to explain to me what the error is saying, and how I can solve my case? Ty!

Found this SO thread but no clue neither FastAPI middleware peeking into responses


Here is one solution:

 if status == 200 and 'Set-Cookie' in headers:
     return JSONResponse(content=True, headers={
         'Set-Cookie' : headers['Set-Cookie']
     })
 else:
     return status

Ty @HernánAlarcón

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda