当前位置: 首页 > 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.gsyq.cn/news/143828.html

相关文章:

  • 2025 最新沧州漏水维修公司TOP5 评测!优质企业及施工单位选择指南,精准检测 + 长效治理权威榜单发布,技术赋能守护建筑安全 - 全局中转站
  • spi
  • 2025论文神器终极指南:8款免费工具实测,30分钟生成5万字高信度问卷数据!
  • Pr字幕样式如何统一修改?简单3步,新手也能一次改完
  • https代理服务器(七)假如被CA出卖
  • 网工抓包高手必备:tcpdump 玩明白才是王道!(语法参数 + 应用场景一篇搞懂)
  • Burp Suite 插件 | SQL 注入自定义扫描和分析
  • 大模型选型神器:DeepSeek 辅助分析业务需求匹配最优 AI 工具
  • DNF私服增幅31的bug?这个你知道吗?
  • 新手自学黑客技术不踩雷指南:2025 最新亲测学习网站合集,助力高效入门与提升
  • 爆肝整理!专为实战挖洞打造的自动化扫描工具,渗透测试效率翻倍
  • SELinux 与 AppArmor 的配置,增强系统安全性
  • Excel超实用技能,一键批量快速合并相同内容单元格,瞬间搞定
  • 圣【牛客tracker 每日一题】
  • 文档结构化系统通过OCR、NLP、知识图谱、多模态融合等技术的综合应用,彻底改变档案管理的本质
  • Linux系统编程——网络:从 OSI 到 UDP 通信实践
  • 屹晶微 EG2104D 600V耐压、宽压输入、带SD关断功能的高性价比半桥栅极驱动器技术解析
  • IPC之消息队列(1)
  • windows11家庭版,解决找不到gpedit.msc文件的问题,打开组策略编辑器。 - 风潇潇兮-Missmen
  • 2025激光切割机哪家好?激光切割设备厂家推荐综合实力榜单 - 栗子测评
  • 动漫之家系统设计与实现
  • GitOps 详解与工具链全解析
  • 【网工技术实验】华为S5700交换机堆叠配置实验案例
  • 深度学习入门
  • 线性拟合模型
  • 高反光条码读取技术突破:京元智能DPM PDA偏振光源与多光谱引擎解析
  • 2025.12.23总结
  • 2025德国留学机构综合实力榜单 - 栗子测评
  • 考研初试落幕,前方星光依旧!大学生考研初试后超全攻略
  • 鱼竿排名第一名到第十名推荐:十大钓鱼竿品牌排行榜,国产十大鱼竿排名盘点 - 品牌2026