JDKF测试
This commit is contained in:
BlueSkyXN 2024-10-14 14:09:47 +08:00
parent 1ff3fa59dc
commit 4f14a08a9e
4 changed files with 215 additions and 0 deletions

View File

@ -73,6 +73,7 @@
<option value="ipfs">IPFS-去中心化多网关兼容</option>
<option value="58img">58img-定期删图</option>
<option value="aliex">AliEx-国内CDN国外Akamai</option>
<option value="jdkf">JDKF-国内BGP</option>
<option value="tgphimg">TGPH-Debug通道</option>
<option value="qst8">qst8-国内CDN</option>
<option value="vviptuangou">vviptuangou-国内CDN</option>

View File

@ -0,0 +1,78 @@
async function handlejdkfRequest(request) {
console.log('Request received for jdkf upload:', request.url);
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 });
}
try {
// 解析请求中的表单数据
const formData = await request.formData();
const file = formData.get('image'); // 前端上传的字段名为 'image'
if (!file) {
return new Response('No file uploaded', { status: 400 });
}
// 创建新的 FormData用于发送到目标接口
const newFormData = new FormData();
newFormData.append('files', file, file.name); // 目标接口要求字段名为 'files'
// 设置目标接口所需的头部信息
const targetHeaders = {
'Accept': 'application/json',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Priority': 'u=1, i',
'Sec-CH-UA': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
'Sec-CH-UA-Mobile': '?0',
'Sec-CH-UA-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'none',
'X-Requested-With': 'XMLHttpRequest'
};
// 目标上传接口的 URL
const targetUrl = 'https://kefu-jtalk.jd.com/jtalk/hfive/resource/image/upload';
// 发送请求到目标接口
const response = await fetch(targetUrl, {
method: 'POST',
headers: targetHeaders,
body: newFormData
});
if (!response.ok) {
const errorText = await response.text();
console.error(`Upload failed: Status ${response.status}, Body: ${errorText}`);
return new Response(`Upload failed: ${errorText}`, { status: response.status });
}
// 解析目标接口的响应
const responseData = await response.json();
console.log('Response from jdkf API:', responseData);
// 从响应中提取图片 URL
const uploadUrl = responseData?.data?.[0]?.url;
if (uploadUrl) {
// 成功,返回图片 URL
return new Response(uploadUrl, {
status: 200,
headers: {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*' // 根据需要调整 CORS 策略
}
});
} else {
console.error('URL not found in response:', responseData);
return new Response('Upload succeeded but no URL returned', { status: 500 });
}
} catch (error) {
console.error('Error in handleJdkfRequest:', error);
return new Response('Internal Server Error', { status: 500 });
}
}

View File

@ -65,6 +65,9 @@ async function handleRequest(request) {
case '/upload/aliex':
response = await handleAliExpressRequest(request);
break;
case '/upload/jdkf':
response = await handlejdkfRequest(request);
break;
default:
response = new Response('Not Found', { status: 404 });
break;
@ -720,3 +723,81 @@ async function handleRequest(request) {
}
}
async function handlejdkfRequest(request) {
console.log('Request received for jdkf upload:', request.url);
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 });
}
try {
// 解析请求中的表单数据
const formData = await request.formData();
const file = formData.get('image'); // 前端上传的字段名为 'image'
if (!file) {
return new Response('No file uploaded', { status: 400 });
}
// 创建新的 FormData用于发送到目标接口
const newFormData = new FormData();
newFormData.append('files', file, file.name); // 目标接口要求字段名为 'files'
// 设置目标接口所需的头部信息
const targetHeaders = {
'Accept': 'application/json',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Priority': 'u=1, i',
'Sec-CH-UA': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
'Sec-CH-UA-Mobile': '?0',
'Sec-CH-UA-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'none',
'X-Requested-With': 'XMLHttpRequest'
};
// 目标上传接口的 URL
const targetUrl = 'https://kefu-jtalk.jd.com/jtalk/hfive/resource/image/upload';
// 发送请求到目标接口
const response = await fetch(targetUrl, {
method: 'POST',
headers: targetHeaders,
body: newFormData
});
if (!response.ok) {
const errorText = await response.text();
console.error(`Upload failed: Status ${response.status}, Body: ${errorText}`);
return new Response(`Upload failed: ${errorText}`, { status: response.status });
}
// 解析目标接口的响应
const responseData = await response.json();
console.log('Response from jdkf API:', responseData);
// 从响应中提取图片 URL
const uploadUrl = responseData?.data?.[0]?.url;
if (uploadUrl) {
// 成功,返回图片 URL
return new Response(uploadUrl, {
status: 200,
headers: {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*' // 根据需要调整 CORS 策略
}
});
} else {
console.error('URL not found in response:', responseData);
return new Response('Upload succeeded but no URL returned', { status: 500 });
}
} catch (error) {
console.error('Error in handleJdkfRequest:', error);
return new Response('Internal Server Error', { status: 500 });
}
}

View File

@ -0,0 +1,55 @@
import requests
# 上传的目标URL
url = "https://kefu-jtalk.jd.com/jtalk/hfive/resource/image/upload"
# 请求头设置
headers = {
"accept": "application/json",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7",
"cache-control": "no-cache",
"pragma": "no-cache",
"priority": "u=1, i",
"sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "none",
"x-requested-with": "XMLHttpRequest"
}
# 本地文件路径和上传时的文件名
file_path = r"F:\Download\20240416-164224.jpg"
upload_filename = "test.jpg"
# 打开文件并准备上传
with open(file_path, 'rb') as file:
files = {
"files": (upload_filename, file, "image/jpeg")
}
try:
# 发送POST请求
response = requests.post(url, headers=headers, files=files)
# 检查响应状态码
if response.status_code == 200:
# 解析并打印JSON响应
response_data = response.json()
print("上传成功!响应内容如下:")
print(response_data)
# 提取URL
upload_url = response_data.get('data', [{}])[0].get('url', None)
if upload_url:
print("提取的上传URL为")
print(upload_url)
else:
print("未找到有效的上传URL。")
else:
print(f"上传失败,状态码:{response.status_code}")
print("响应内容:")
print(response.text)
except requests.exceptions.RequestException as e:
print("上传过程中发生错误:", e)