问题描述: 我正在使用 Node.js 和 Express 构建 FHIR 服务器,并尝试通过发送 POST 请求来创建新的患者资源。但是,我遇到了以下问题:尽管使用不同的 REST 客户端(Thunder 客户端和 VS Code REST 客户端)发送请求,但服务器收到的请求正文未定义。
采取的步骤:
检查请求正文格式:我验证了 JSON 请求正文的格式正确,包含所需字段,包括“resourceType”、“name”、“gender”和“birthDate”。
客户端配置:我确保两个 REST 客户端中的标头(包括“Content-Type”和“Accept”)均正确设置为“application/fhir+json”以及适当的 fhirVersion。
请求方法:我确认我正在使用 POST 方法进行请求。
端点 URL:我验证端点 URL 是否设置为 http://localhost:3000/4_0_0/Patient 以匹配服务器路由。
Content-Length Header:content-length header 貌似是客户端自动设置的,我没有手动修改。
REST 客户端设置:我检查了两个 REST 客户端中可能影响请求正文发送方式的任何特定设置或选项。我没有发现任何可能导致该问题的设置。
重新启动服务器:我尝试重新启动服务器,以防服务器代码的更改需要生效。
尝试不同的客户端:我尝试使用 Thunder 客户端和 VS Code REST 客户端发送请求,以查看问题是否特定于某个客户端,但问题仍然存在于两个客户端中。
检查错误:我在客户端控制台或日志中查找错误消息或警告,但没有找到任何可以深入了解问题的内容。
尽管做出了这些努力,我仍然面临这样的问题:服务器在尝试创建患者资源时收到未定义的请求正文。
const { VERSIONS } = constants;
const express = require('express');
let config = {
profiles: {
patient: {
service: './patient.service.js',
versions: [VERSIONS['4_0_0']],
},
},
};
let server = initialize(config);
let logger = loggers.get('default');
// Create an Express application
const app = express();
// In-memory data store for Patient resources
const patientDataStore = [];
// creating a Patient resource
app.post('/4_0_0/Patient', (req, res) => {
console.log('Received POST request with data:', req.body);
console.log('Request headers:', req.headers);
const patientData = req.body;
console.log('Received POST request with data:', patientData);
// Perform validation, data storage, etc.
// For simplicity, we'll just push the patientData to the in-memory store
patientDataStore.push(patientData);
console.log('Patient data stored:', patientDataStore);
// Send a response indicating success (HTTP status code 201 for resource creation)
res.status(201).json({ message: 'Patient resource created successfully' });
});
// Define route handlers for GET requests to the URLs
app.get('/4_0_0/', (req, res) => {
res.send('GET request to /4_0_0/');
});
app.get('/4_0_0/Patient', (req, res) => {
console.log('Data in patientDataStore:', patientDataStore);
res.json(patientDataStore);
});
// Define a route to retrieve and display patient data
app.get('/patientData', (req, res) => {
console.log('GET request to /patientData');
console.log('Data in patientDataStore:', patientDataStore);
res.json(patientDataStore);
});
const fhirMiddleware = (req, res, next) => {
server(req, res, next);
};
app.use('/fhir', fhirMiddleware);
const PORT = 3000;
app.listen(PORT, () => {
logger.info(`Starting the FHIR Server at localhost:${PORT}`);
});```
添加我的评论作为答案,这样这个问题就可以结束了。
“应用程序/fhir+json”
你可以(在客户端)尝试“”application/json”吗?
我不确定 fhir+json 是否“被理解”。
您是否尝试过仅发布一个简单的 json
{ "hello": "world" }
您的新休息服务?
如果这有帮助,请标记“作为答案”,这样这个问题就不会出现在“未回答的问题”中。
另外,请将您原来的问题附加到您发现的问题/答案中。这样将来的人也可以从这个问题/答案中学习。