金店参考价格:Python / cURL / Node.js / Java 多语言调用示例
# 金店参考价格:Python / cURL / Node.js / Java 多语言调用示例
> 接口 2145(金店参考价格)· 免费 · 四语言示例 · 适用人群:开发者 · 阅读时间约 4 分钟
## 核心要点
- 覆盖 Python(requests)、cURL、Node.js(fetch)、Java(HttpURLConnection) 四语言。
- 两个接入点各一套,密钥用 YOUR_APPKEY 占位,示例值取自文档。
- 均含超时与 ret_code 判断,替换占位符即可运行。
## Why:这跟我有什么关系
把金店参考价格接口接进项目,最关心的就是「我用的语言怎么写」。本文给四语言完整可运行片段。
## What:前置条件与接口速览
**公共约定**
| 项 | 值 |
|---|---|
| 2145-1 地址 | `https://route.showapi.com/2145-1?appKey=YOUR_APPKEY` |
| 2145-2 地址 | `https://route.showapi.com/2145-2?appKey=YOUR_APPKEY` |
| 鉴权 | query 参数 appKey |
| 超时 | 建议 10s |
## How:接入步骤
**① Python(requests)**
```python
import requests
url = "https://route.showapi.com/2145-1"
resp = requests.post(
url,
params={"appKey": "YOUR_APPKEY"},
headers={"content-type": "application/x-www-form-urlencoded"},
timeout=10,
)
data = resp.json()
if data.get("showapi_res_code") == 0 and data["showapi_res_body"]["ret_code"] == 0:
for item in data["showapi_res_body"]["data"]:
print(item["_id"], item["brand"])
else:
print("查询失败:", data.get("showapi_res_error"))
```
```python
import requests
brand_id = "5da958ead3fb8b0eefc6a3d2" # 来自「支持金店」接入点返回的 _id
url = "https://route.showapi.com/2145-2"
resp = requests.post(
url,
params={"appKey": "YOUR_APPKEY"},
data={"id": brand_id},
headers={"content-type": "application/x-www-form-urlencoded"},
timeout=10,
)
body = resp.json()["showapi_res_body"]
if body["ret_code"] == 0:
print(body["brand"], "黄金", body["goldPrice"], "铂金", body["platinumPrice"], body["auLastDate"])
elif body["ret_code"] == -1:
print("未找到该品牌:", body.get("remark"))
```
**② cURL**
```bash
curl -X POST "https://route.showapi.com/2145-1?appKey=YOUR_APPKEY" \
-H "content-type: application/x-www-form-urlencoded"
```
```bash
curl -X POST "https://route.showapi.com/2145-2?appKey=YOUR_APPKEY" \
-H "content-type: application/x-www-form-urlencoded" \
-d "id=5da958ead3fb8b0eefc6a3d2"
```
**③ Node.js(fetch)**
```javascript
const resp = await fetch("https://route.showapi.com/2145-1?appKey=YOUR_APPKEY", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
});
const data = await resp.json();
if (data.showapi_res_code === 0 && data.showapi_res_body.ret_code === 0) {
data.showapi_res_body.data.forEach((it) => console.log(it._id, it.brand));
}
```
```javascript
const brandId = "5da958ead3fb8b0eefc6a3d2";
const resp = await fetch("https://route.showapi.com/2145-2?appKey=YOUR_APPKEY", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ id: brandId }),
});
const body = (await resp.json()).showapi_res_body;
if (body.ret_code === 0) {
console.log(body.brand, "黄金", body.goldPrice, "铂金", body.platinumPrice, body.auLastDate);
} else if (body.ret_code === -1) {
console.log("未找到该品牌:", body.remark);
}
```
**④ Java(HttpURLConnection)**
```java
import java.io.*;
import java.net.*;
import java.net.URLEncoder;
String brandId = "5da958ead3fb8b0eefc6a3d2";
String url = "https://route.showapi.com/2145-2?appKey=YOUR_APPKEY";
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("POST");
c.setRequestProperty("content-type", "application/x-www-form-urlencoded");
c.setDoOutput(true);
try (OutputStream os = c.getOutputStream()) {
os.write(("id=" + URLEncoder.encode(brandId, "UTF-8")).getBytes("UTF-8"));
}
// 读取响应 JSON -> showapi_res_body.ret_code / goldPrice / platinumPrice / auLastDate
int code = c.getResponseCode();
System.out.println("HTTP " + code);
```
## 返回示例与解析
**2145-2 返回示例**
```python
{
"showapi_res_error": "",
"showapi_fee_num": 1,
"showapi_res_code": 0,
"showapi_res_id": "67a5aaf5fb638c27e15696c2",
"showapi_res_body": {
"ret_code": 0,
"platinumPrice": 409,
"_id": "5da958ead3fb8b0eefc6a3d2",
"remark": "",
"auLastDate": "2025-02-07",
"showapi_fee_code": 0,
"ptLastDate": "2025-02-07",
"brand": "周大生",
"goldPrice": 869
}
}
```
所有语言解析逻辑一致:先判断 `showapi_res_code==0` 与 `ret_code==0`,再读 `goldPrice`/`platinumPrice`。
## 进阶 / 边界
- 四语言均用 query 参数 `appKey` 鉴权,不要把 AppKey 写死在前端代码。
- Java 示例仅演示请求建立,解析请配合任意 JSON 库(如 Jackson/Gson)读取 `showapi_res_body`。
- 批量/缓存写法见[缓存策略](https://www.showapi.com/guides/gold-price-cache-cost-2145) 与[高并发架构](https://www.showapi.com/guides/gold-price-architecture-2145)。
## FAQ
**Q:AppKey 放哪安全?**
服务端/云函数内,用环境变量注入;前端直连会泄露密钥。
**Q:Java 怎么解析返回?**
用 HttpURLConnection 拿到输入流后,交由 Jackson/Gson 解析 showapi_res_body 对象。
**Q:Node.js 必须用 fetch 吗?**
示例用原生 fetch(Node 18+),也可用 axios 等库,结构相同。
## 相关能力 / 下一步阅读
- [金店参考价格:5 分钟接入](https://www.showapi.com/guides/gold-price-quickstart-2145)
- [金店参考价格:返回字段与状态码全解](https://www.showapi.com/guides/gold-price-response-codes-2145)
- [金店参考价格:两步调用工作流](https://www.showapi.com/guides/gold-price-two-step-2145)
- **本系列共 12 篇**:查看[金店参考价格 · 官方指南总目录](https://www.showapi.com/guides/gold-price-guides-2145)