Skip to content

Commit da65300

Browse files
authored
fix POST
1 parent 2aad993 commit da65300

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

index.js

+60-48
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,96 @@
1-
const express = require('express');
2-
const axios = require('axios');
3-
const cors = require('cors');
1+
const express = require("express");
2+
const axios = require("axios");
3+
const cors = require("cors");
44

55
const app = express();
66
app.use(express.json());
7-
app.use(cors({
8-
origin: '*',
9-
methods: ['GET', 'POST'],
10-
allowedHeaders: ['Content-Type']
11-
}));
7+
app.use(cors());
128

13-
const endpoint = 'https://us-central1-chat-for-chatgpt.cloudfunctions.net/basicUserRequestBeta';
9+
const endpoint =
10+
"https://us-central1-chat-for-chatgpt.cloudfunctions.net/basicUserRequestBeta";
1411

15-
function sendResponse(res, status, code, message) {
16-
res.setHeader('Content-Type', 'application/json');
17-
res.status(status).send(JSON.stringify({ status, code, message }, null, 2));
12+
function sendResponse(res, status, message) {
13+
res.setHeader("Content-Type", "application/json");
14+
res.status(status).send(JSON.stringify({ status, message }, null, 2));
1815
}
1916

20-
app.get('/', async (req, res) => {
17+
app.get("/", async (req, res) => {
2118
const text = req.query.text;
2219

2320
if (!text) {
24-
sendResponse(res, 400, 400, 'Please enter text parameter');
21+
sendResponse(res, 400, "Please enter text parameter");
2522
return;
2623
}
2724

2825
try {
29-
const response = await axios.post(endpoint, {
30-
data: {
31-
message: text
26+
const response = await axios.post(
27+
endpoint,
28+
{
29+
data: {
30+
message: text,
31+
},
32+
},
33+
{
34+
headers: {
35+
Host: "us-central1-chat-for-chatgpt.cloudfunctions.net",
36+
Connection: "keep-alive",
37+
Accept: "*/*",
38+
"User-Agent":
39+
"com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5",
40+
"Accept-Language": "en",
41+
"Content-Type": "application/json; charset=UTF-8",
42+
},
3243
}
33-
}, {
34-
headers: {
35-
'Host': 'us-central1-chat-for-chatgpt.cloudfunctions.net',
36-
'Connection': 'keep-alive',
37-
'Accept': '*/*',
38-
'User-Agent': 'com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5',
39-
'Accept-Language': 'ar',
40-
'Content-Type': 'application/json; charset=UTF-8'
41-
}
42-
});
44+
);
4345

4446
const result = response.data.result.choices[0].text;
45-
sendResponse(res, 200, 200, result);
47+
sendResponse(res, 200, result);
4648
} catch (error) {
47-
sendResponse(res, 403, 403, 'Error connecting to openai');
49+
sendResponse(res, 403, "Error connecting to openai");
4850
}
4951
});
5052

51-
app.post('/', async (req, res) => {
53+
app.post("/", async (req, res) => {
5254
const text = req.body.text;
5355

5456
if (!text) {
55-
sendResponse(res, 400, 400, 'Please enter text parameter');
57+
sendResponse(res, 400, "Please enter text parameter");
5658
return;
5759
}
5860

5961
try {
60-
const response = await axios.post(endpoint, {
61-
data: {
62-
message: text
63-
}
64-
}, {
65-
headers: {
66-
'Host': 'us-central1-chat-for-chatgpt.cloudfunctions.net',
67-
'Connection': 'keep-alive',
68-
'Accept': '*/*',
69-
'User-Agent': 'com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5',
70-
'Accept-Language': 'ar',
71-
'Content-Type': 'application/json; charset=UTF-8'
62+
const response = await axios.post(
63+
endpoint,
64+
{
65+
data: {
66+
message: text,
67+
},
68+
},
69+
{
70+
headers: {
71+
Host: "us-central1-chat-for-chatgpt.cloudfunctions.net",
72+
Connection: "keep-alive",
73+
Accept: "*/*",
74+
"User-Agent":
75+
"com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5",
76+
"Accept-Language": "en",
77+
"Content-Type": "application/json; charset=UTF-8",
78+
},
7279
}
73-
});
80+
);
7481

7582
const result = response.data.result.choices[0].text;
76-
sendResponse(res, 200, 200, result);
83+
sendResponse(res, 200, result);
7784
} catch (error) {
78-
sendResponse(res, 403, 403, 'Error connecting to openai');
85+
sendResponse(res, 403, "Error connecting to openai");
7986
}
8087
});
8188

89+
app.use((err, req, res, next) => {
90+
console.error(err.stack);
91+
sendResponse(res, 500, "Something broke!");
92+
});
93+
8294
app.listen(3000, () => {
83-
console.log('ChatGPT API is running on port 3000');
84-
});
95+
console.log("ChatGPT API is running on port 3000");
96+
});

0 commit comments

Comments
 (0)