Skip to content

Commit 8f2880d

Browse files
committed
🐛 Fix error if info is empty
1 parent 7cc5c97 commit 8f2880d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Diff for: src/main.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,23 @@ async fn proxy(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
8484
// Create new uri
8585
let uri = Uri::from_str(&format!("{}{}", CONFIG.chatgpt_url, path_and_query,)).unwrap();
8686
println!("uri: {:?}", uri);
87+
// if req header Authorization is null, set empty
88+
let authorization = if let Some(value) = req.headers().get("Authorization") {
89+
value.to_str().unwrap()
90+
} else {
91+
""
92+
};
93+
let content_type = if let Some(value) = req.headers().get("Content-Type") {
94+
value.to_str().unwrap()
95+
} else {
96+
"application/json"
97+
};
98+
8799
let request_builder = Request::builder().method(req.method())
88100
.uri(uri)
89101
.header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50")
90-
.header("Authorization", req.headers().get("Authorization").unwrap().to_str().unwrap_or_else(|_| "null"))
91-
.header("Content-Type", req.headers().get("Content-Type").unwrap().to_str().unwrap_or_else(|_| "application/json"));
102+
.header("Authorization", authorization)
103+
.header("Content-Type", content_type);
92104

93105
let response = client
94106
.request(request_builder.body(req.into_body()).unwrap())

0 commit comments

Comments
 (0)