我尝试使用symfony 3.4中的guzzlehttp / guzzle捆绑包从后端向我的NodeJS测试服务器发出HTTP请求。我试图用邮递员和cURL测试我的nodeJS服务器,它可以工作,但不能在我的后端工作。
这是我的代码:
/**
* @POST("/partner/searchNurseToUser")
*/
public function searchNurseAction(Request $request)
{
$client = new Client(['base_uri' => 'http://172.18.0.5:9000']);
$nurseFirstname = htmlspecialchars($request->request->get('nurseFirstname'));
$nurseLastname = htmlspecialchars($request->request->get('nurseLastname'));
$nursePhone = htmlspecialchars($request->request->get('nursePhoneNumber'));
$nursePostalCode = htmlspecialchars($request->request->get('nursePostalCode'));
$nurseCity = htmlspecialchars($request->request->get('nurseCity'));
try {
$response = $client->request('POST', 'partner/pros/', [
'json' => [
'name' => $nurseFirstname . ' ' . $nurseLastname,
'city' => $nurseCity,
'phone' => $nursePhone,
'zipcode' => $nursePostalCode
]
]);
const bodyParser = require('body-parser');
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// Define routes
app.use('/partner/', require('./routes/api/partner'));
const port = 9000;
app.listen(port, function() {
console.log('Listening on port 9000!')
})
const router = express.Router();
router.post('/pros', async (req, res) => {
const { name, email, phone, city, zipcode } = req.body;
try {
let response = {};
if (name || email || phone || city || zipcode) {
response = {
"data": [
{
"civility": "F",
"firstname": "Adeline",
"lastname": "Dupont"
}
]
}
} else {
response = [];
}
return res.json(response).status(200);
} catch (err) {
console.error(err.message);
res.status(500).send('Server error');
}
})
module.exports = router;
但是邮差返回此错误消息:
Error GuzzleHttp Message Error "Resources are not supported in serialized data. Path: GuzzleHttp\\Psr7\\Stream".
谢谢您的帮助!
答案 0 :(得分:0)
我找到了解决方案!
只需在我的函数searchNurseAction的返回中使用JsonResponse!
/**
* @POST("/partner/searchNurseToUser")
*/
public function searchNurseAction(Request $request)
{
$client = new Client(['base_uri' => 'http://172.18.0.5:9000']);
$nurseFirstname = htmlspecialchars($request->request->get('nurseFirstname'));
$nurseLastname = htmlspecialchars($request->request->get('nurseLastname'));
$nursePhone = htmlspecialchars($request->request->get('nursePhoneNumber'));
$nursePostalCode = htmlspecialchars($request->request->get('nursePostalCode'));
$nurseCity = htmlspecialchars($request->request->get('nurseCity'));
$response = $client->request('POST', 'partner/pros/', [
'form_params' => [
'name' => $nurseFirstname . ' ' . $nurseLastname,
'city' => $nurseCity,
'phone' => $nursePhone,
'zipcode' => $nursePostalCode,
'job' => 1
]
]);
try {
if($response->getStatusCode() === 200 || $response->getStatusCode() === 204){
$nurses = $response->getBody();
$ret = array(
'nurses' => $nurses,
'status' => 200,
);
return new JsonResponse($nurses, 200, [], true);
}
} catch (\Exception $e) {
$ret = array(
'error' => $e->getMessage(),
);
return new JsonResponse($e, 500, [], true);
}
}
而不是:
/**
* @POST("/partner/searchNurseToUser")
*/
public function searchNurseAction(Request $request)
{
$client = new Client(['base_uri' => 'http://172.18.0.5:9000']);
$nurseFirstname = htmlspecialchars($request->request->get('nurseFirstname'));
$nurseLastname = htmlspecialchars($request->request->get('nurseLastname'));
$nursePhone = htmlspecialchars($request->request->get('nursePhoneNumber'));
$nursePostalCode = htmlspecialchars($request->request->get('nursePostalCode'));
$nurseCity = htmlspecialchars($request->request->get('nurseCity'));
$response = $client->request('POST', 'partner/pros/', [
'form_params' => [
'name' => $nurseFirstname . ' ' . $nurseLastname,
'city' => $nurseCity,
'phone' => $nursePhone,
'zipcode' => $nursePostalCode,
'job' => 1
]
]);
try {
if($response->getStatusCode() === 200 || $response->getStatusCode() === 204){
$nurses = $response->getBody();
$ret = array(
'nurses' => $nurses,
'status' => 200,
);
$view = $this->view($ret);
}
} catch (\Exception $e) {
$ret = array(
'error' => $e->getMessage(),
);
$view = $this->view($ret, 500);
}
return $this->handleView($view);
}
对不起,我的问题还不完整。谢谢你