#Status code from response

26 messages · Page 1 of 1 (latest)

hidden kernel
#

Does anyone know how I can get the API's response status code successfully?
I already tried:
# Create the headers, providing the API key and version
auth = f'Bearer {openai.api_key}'
headers = {
'Authorization': auth,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
check_status = requests.get("https://api.openai.com/v1/engines/davinci/completions", headers=headers)
status_code = check_status.status_code
# Check the response status
print("Response status of OpenAI:", status_code)
# Check the response text
print("Response text:", check_status.text)'
but I get 405 error :/

elfin flume
#

I would recommend not using requests and instead using the OpenAI module which does all the heavy lifting for you

hidden kernel
#

I do use it to get the response but I didn't find any alternative to response.status_code as the requests package have

elfin flume
hidden kernel
#

do you know how can I implement it with the open ai module?

elfin flume
#

OpenAI handles the response form you

#

*for you

#

Import openai

#

The examples in the API documentation include using the module for their examples

hidden kernel
#

can I get the status code? from the open ai module?

#

I need the status code for unit testing

elfin flume
#

I honestly don't know how it handles any bad server codes

hidden kernel
elfin flume
#

I'm sure it does,. I just don't know

#

You need to change it to requests.post first and the deliver a payload

#

If you strictly just want to try the server to see if it's available

#
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Model.list()
#

Easy with the module

#
curl https://api.openai.com/v1/models \
  -H 'Authorization: Bearer YOUR_API_KEY'

With curl, but the same configuration can be used with Requests

hidden kernel
#

Thanks, this is nice to check the availability of the server, but I need the status codes :/

elfin flume
#

What kind of unit tests do you hope to accomplish? The only codes I could imagine with a functional service would be 5x codes if the server is down

hidden kernel
#

I want to print the status code of the response while I'm running testing with py test

#

I found that the API can response status code I dont know how

elfin flume
#

Maybe it's thrown as an exception? Have you tried sending a nonsensical request using the module and seeing what returns?

#

I'm sure they send the status code. I just have never tried it