发布到symfony端点的Node.js表单数据为NULL



几天前,我发现使用"内容类型":"application/x-www-form-urlencoded"发布数据不会让我发送某些语言中使用的字符,如ñ、ç、á、é、í、ó、ú和其他非ASCII字符。它返回[ERR_INVALID_CHAR]。

我在谷歌上搜索了一下,发现为了发布大量数据和非ASCII字符,我应该使用"内容类型":"多部分/表单数据"(基本上是一种表单(。

因此,由于我发布的信息不是在模板表单中收集的,我不得不直接在node.js服务器应用程序中的一个函数中构建表单数据对象。这是我的node.js代码:

const formData = require('form-data');
const https = require('https');
const form = new formData();
form.append('name', 'ñámeç');
let options = {
hostname: 'dev.mydomain.com',
path: '/processdata',
method: 'POST',
headers: form.getHeaders()
}
let req = https.request(options, res=>{
console.log(res.statusCode);
res.on('data', function(chunk){
console.log(''+chunk);
});
res.on('end', function(){
console.log('end');
});
});
req.on('error',function(e){
console.log(e);
})
form.pipe(req);

然后,dev.mydomain.com/processdata中的API构建在Symfony5中。在我从"内容类型":"application/x-www-form-urlencoded"更改为"内容类型":"多部分/表单数据"之前,代码为:

public function processdata(Request $request)
{
// Get post data
$json = $request->get('json', null);
// Json decode data
$guest = json_decode($json);
... DO SOME STUFF ...
$data = "RESULTING STRING";
return new JsonResponse($data);
}

我已将其更改为:

public function processData(Request $request)
{
// This is how I expected it would be:
var_dump($request->request->get('name'));
// This is just in case it was arriving as GET instead of POST (yes, I'm desperate):
var_dump($request->query->get('name'));
//  This is just in case it was arriving as a form object.
var_dump($request->query->get('form')['name']);
// This is also a desperate test, but I found some forums pointing this out: 
var_dump($request->get('name'));
// Trying to print the request contents:
print_r($request->request);
// Since all the $request calls in the var_dumps returned NULL or EMPTY data, I forced the enpoint to return a string to make sure I had a response.
// Forcing a string for the callback:
$data = "FORCED RESULTING STRING";
return new JsonResponse($data);
}

然后我运行node.js应用程序,输出为:

Claire:Test claire$ node test.js
200
NULL
NULL
NULL
NULL
SymfonyComponentHttpFoundationParameterBag Object
(
[parameters:protected] => Array
(
)
)
"FORCED RESULTING STRING"
end

有人能救我吗?我被困在这里好几天了。我在这里读到的所有内容都不适用于我的情况,或者我只是没有意识到其中的区别。

如果我缺少任何数据,你可能需要我澄清,请告诉我。

提前非常感谢,我希望你们都安全健康。

感谢您的回复,@qdvequipe!!我不确定,但你照亮了我的路!

我再次检查了表单数据npm文档,显然,form.getHeaders()只会设置多部分/表单数据头。

我记得Symfony需要设置Content-Length标头。否则,发布的数据将变为NULL。

然后,我再次尝试使用form.submit(((我以前使用过,但显然我做得不对(,它为我设置了内容长度

const formData = require('form-data');
const https = require('https');
const form = new formData();
form.append('name', 'ñámeç');
form.submit('https://dev.mydomain.com/processdata', function(err,res){
console.log(res.statusCode);
res.on('data', function(chunk){
console.log(''+chunk);
});
res.on('end', function(){
console.log('end');
});
res.resume();
});

它返回:

Claire:Test claire$ node test.js
200
string(8) "ñámeç"
NULL
NULL
string(8) "ñámeç"
SymfonyComponentHttpFoundationParameterBag Object
(
[parameters:protected] => Array
(
[name] => ñámeç
)
)
"FORCED RESULTING STRING"
end

$request->request->get('name'(现在捕获"name"post字段。

我不知道现在该怎么办。我应该把我的答复标记为有效吗?也许你可以创建一个回复,这样我就可以标记你的回复,@qdvippe!!!非常感谢!祝你今天愉快!

最新更新