HTTPS (HTTP+TLS 加密)

过程: 发请求到服务器地址 -> 服务器处理并返回一个响应
content: JSON

组成:

Method 方法 URL 地址 Headers请求头 Body 请求体

Method : POST
URL :

https:// 协议 scheme
 api.openai.com 主机
 /v1 版本前缀
 /response endpoint 路由 资源路径

Header

 鉴权 Authorization 
content type 

Body 问题指令 json

基础请求(cURL (Client URL 命令行请求工具) )

curl -X POST
https://api.openai.com/v1/responses \
    -H "Authorization: Bearer
    $OPENAI_API_KEY" \
    -H "Content-Type: application/json"
    -d '{
    "model": "gpt-5.4",
    "instructions":"You are a helpful assistant.",
    "input": "Explain API to me."
    }'
curl -d 默认就是 post ,可以忽略 -X POST

Response:

{
    "id": "resp_xxx",
    "status": "",
    "output": [],
    "usage": {}
}

status 状态码

其他主流鉴权
XAPI-Key :your_api_key

早期HTTP 有很多非标准、自定义 header X代表 EXTRA

python示例:
引入 requests 库
GET 示例

import requests

url = "https://api.example.com/status"

response = requests.get(url)

print(response.status_code)  # HTTP 状态码,比如 200、401、500
print(response.text)         # 原始返回内容

POST openai LLM 示例

import os # operating system
import requests # requests 网络请求

api_key = os.getenv("OPENAI_API_KEY")

url = "https://api.openai.com/v1/responses"

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
}

payload = { # 负载 请求体 payload
    "model": "gpt-5.2",
    "input": "Explain API in simple words.",
}

# 自动加 Content-Type,自动转 JSON 字符串
response = requests.post(url, headers=headers, json=payload) 

print(response.status_code)
print(response.json())

本文归档于个人知识库 · 领域:CS & AI · 模块:04 Project_CS_AI/AI apps