当前位置: 首页 > news >正文

postman测试salesforce接口

好的,让我教你怎么用 Postman 测试接口。以下是详细步骤:

📌 第一步:设置认证

1. 获取访问令牌 (Access Token)

POST https://login.salesforce.com/services/oauth2/token
Content-Type: application/x-www-form-urlencodedgrant_type=password
&client_id=你的Consumer Key
&client_secret=你的Consumer Secret
&username=你的用户名
&password=你的密码+安全令牌

示例:

username: yourname@example.com
password: yourpasswordyoursecuritytoken

2. 获取响应

你会得到类似这样的响应:

{"access_token": "00Dxxxxxxxxxxxxxx!xxxxxxxx","instance_url": "https://your-domain.my.salesforce.com","id": "https://login.salesforce.com/id/00Dxxxxxxxxxxxxxx/005xxxxxxxxxxxxxx","token_type": "Bearer","issued_at": "1672531200","signature": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

📌 第二步:配置 Postman

1. 设置全局变量

在 Postman 的 Environment 中设置:

baseUrl: https://your-domain.my.salesforce.com
access_token: 00Dxxxxxxxxxxxxxx!xxxxxxxx

2. 设置 Headers

Authorization: Bearer {{access_token}}
Content-Type: application/json
Accept: application/json

📌 第三步:测试你的接口

假设你的接口路径是:/services/apexrest/psp/api/v1/*

示例 1:测试患者注册接口

POST {{baseUrl}}/services/apexrest/psp/api/v1/insert

Body (JSON):

{"requestId": "test_001","entityType": "Patient","openId": "test_openid_001","customData": {"patientData": {"name": "张三","phone": "13800138000","birthDate": "1990-01-01"}}
}

示例 2:测试用药提醒接口

POST {{baseUrl}}/services/apexrest/psp/api/v1/insert
{"requestId": "test_002","entityType": "SyncReminder","openId": "test_openid_002","customData": {"reminderData": {"carePlanId": "a0x000000000001","activitys": [{"carePlanActivityName": "降压药","frequency": "Daily","reminderTime": "08:00"}]}}
}

📌 第四步:检查响应

成功响应示例:

{"statusCode": 200,"message": "请求处理完成","results": [{"requestId": "test_001","status": "Success","message": "患者注册成功","customData": {"patientId": "001000000000001AAA"}}]
}

错误响应示例:

{"statusCode": 400,"message": "请求处理失败","results": [{"requestId": "test_001","status": "Fail","message": "参数校验失败: openId不能为空"}]
}

📌 第五步:测试不同的场景

场景 1:参数缺失

{"requestId": "test_003","entityType": "Patient",// 故意不传 openId"customData": {}
}

场景 2:无效数据

{"requestId": "test_004","entityType": "Patient","openId": "test_openid_004","customData": {"patientData": {"phone": "123"  // 无效的手机号}}
}

场景 3:批量请求

{"requestId": "batch_001","entityType": "Batch","items": [{"entityType": "Patient","openId": "batch_openid_1","customData": {}},{"entityType": "SyncReminder","openId": "batch_openid_1","customData": {}}]
}

📌 第六步:查看调试信息

1. 检查 Salesforce 日志

  • 打开 Developer Console
  • 点击 Debug > Open Execute Anonymous Window
  • 查看系统调试日志

2. Postman Console

  • 在 Postman 底部点击 Console
  • 查看请求和响应的详细信息

📌 第七步:自动化测试脚本

你可以在 Postman 中创建 Tests 选项卡:

// 验证响应状态
pm.test("Status code is 200", function () {pm.response.to.have.status(200);
});// 验证响应结构
pm.test("Response has correct structure", function () {var jsonData = pm.response.json();pm.expect(jsonData).to.have.property('statusCode');pm.expect(jsonData).to.have.property('message');pm.expect(jsonData).to.have.property('results');
});// 验证业务逻辑
pm.test("Patient registration successful", function () {var jsonData = pm.response.json();pm.expect(jsonData.results[0].status).to.equal('Success');pm.expect(jsonData.results[0]).to.have.property('customData');
});

📌 第八步:创建测试集合

在 Postman 中:

  1. New Collection → 命名为 "PSP API Tests"
  2. 创建文件夹:Insert APIs、Update APIs、Query APIs
  3. 为每个接口创建请求
  4. 使用 Runner 批量运行所有测试

📌 快速调试技巧

1. 获取接口路径

如果你不知道接口路径:

GET {{baseUrl}}/services/apexrest

这会列出所有 REST 资源。

2. 查看接口定义

检查 Apex 类中的注解:

@RestResource(urlMapping='/psp/api/v1/*')
global class psp_GenericApiService {// ...
}

3. 临时调试代码

在 Apex 类中添加调试:

System.debug('=== 请求参数 ===');
System.debug(JSON.serializePretty(req));

📌 常见问题排查

问题 原因 解决方法
401 Unauthorized Token 过期 重新获取 Token
404 Not Found 接口路径错误 检查 urlMapping
400 Bad Request 参数格式错误 检查 JSON 格式
500 Internal Error Apex 代码异常 查看系统日志

📌 下一步建议

  1. 先测试简单的接口:如患者查询
  2. 逐步增加复杂度:从 insert 到 update
  3. 记录测试用例:保存成功的请求作为测试用例
  4. 编写断言:验证业务逻辑正确性

需要我帮你写具体的测试用例吗?或者你现在遇到了什么问题?

http://www.jsqmd.com/news/130965/

相关文章:

  • 2025 最新沧州漏水维修公司TOP5 评测!优质企业及施工单位选择指南,精准检测 + 长效治理权威榜单发布,技术赋能守护建筑安全 - 全局中转站
  • 【掺铒光纤放大器(EDFA)模型】掺铒光纤放大器(EDFA)分析模型的模拟研究附Matlab代码
  • Vivado下载速率优化技巧(适用于Artix-7)
  • windows11家庭版,无线网卡与蓝牙驱动问题; - 风潇潇兮-Missmen
  • 张雪峰推崇 “干安全有前景”,为何众多网友并不认同?
  • 2025论文神器终极指南:8款免费工具实测,30分钟生成5万字高信度问卷数据!
  • Docker 容器核心配置实战:资源管控、数据卷与容器互联
  • Pr字幕样式如何统一修改?简单3步,新手也能一次改完
  • https代理服务器(八)安卓7
  • 频率响应与信号完整性:一文说清其关联机制
  • 【攻防世界】reverse | tt3441810 详细题解 WP
  • 2025 最新沧州堵漏公司 TOP5 评测!优质企业及施工单位选择指南,技术驱动 + 品质实证权威榜单发布,专业赋能守护建筑安全 - 全局中转站
  • 最新版 Nmap 安装配置教程:2025 超详细实操指南
  • 2025最强福利!9款免费AI论文生成润色工具,提升学术原创性必备!
  • 网工抓包高手必备:tcpdump 玩明白才是王道!(语法参数 + 应用场景一篇搞懂)
  • 本地搭云盘还能远程用?Cloudreve + cpolar内网穿透的简单方案
  • 自容式水听器是什么?偶信科技为您解答
  • https代理服务器(七)假如被CA出卖
  • 哈希表与堆栈:数据存储全解析
  • 大模型选型神器:DeepSeek 辅助分析业务需求匹配最优 AI 工具
  • Burp Suite 插件 | SQL 注入自定义扫描和分析
  • 2025最新沧州漏水检测公司TOP5评测!优质企业及施工单位选择指南,精准探测+长效治理权威榜单发布,技术赋能守护建筑安全生态 - 全局中转站
  • 新手自学黑客技术不踩雷指南:2025 最新亲测学习网站合集,助力高效入门与提升
  • 深入理解C#泛型:从方法到约束
  • DNF私服增幅31的bug?这个你知道吗?
  • 爆肝整理!专为实战挖洞打造的自动化扫描工具,渗透测试效率翻倍
  • 从工程角度看 GlobTek 医疗电缆线束应用
  • 工业控制系统中vivado2021.1安装操作指南
  • 【参数估计】基于WSO算法的太阳能光伏模型参数估计附Matlab代码
  • 好人逆袭:如何赢得尊重又守住善良?