Skip to content

POST JSON w/Authorization Bearer - Getting HTTP 400 & 401 #128

@TheDoctorAI

Description

@TheDoctorAI
  • Python version: 3.9.8
  • Operating System: Debian
  • aiohttp version: ^3.8.0

Description

Trying to connect to openai sse streams by posting JSON. Authorization Bearer is required. HTTP Error 400 keeps happening with the code below. If I move the headers into the options dict, I get a 401 but I don't think that's the right way. My API key works with the openai module. It should work here.

Ref: https://beta.openai.com/docs/developer-quickstart/making-requests

What I Did

    openai_key = ""
    async with sse_client.EventSource(
        'https://api.openai.com/v1/engines/davinci/completions',
        {
            'method': 'post'
            
        },
        json={
            'prompt': "Peper piper picked a peck of",
            'engine': 'davinci',
            'max_tokens': 50,
            'stream': True
        },
        headers={
                'Authorization': f'Bearer {openai_key}'
            }
    ) as event_source:
        try:
            async for event in event_source:
                print(event)
        except ConnectionError:
            pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions