【问题标题】:CURLOPT_POSTFIELDS in NodejsNodejs 中的 CURLOPT_POSTFIELDS
【发布时间】:2021-05-28 20:07:53
【问题描述】:

有一个php脚本:

$host = API_HOST."/getawb";
$username = USERNAME;

$curl_parameters['order_ids'] = array();

$curl_options = array(
    CURLOPT_URL => $host,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query($curl_parameters),
    CURLOPT_HTTP_VERSION => 1.0,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => "$username",
    CURLOPT_HEADER => false
  );

$curl = curl_init();
curl_setopt_array($curl, $curl_options);
$result = curl_exec($curl);
curl_close($curl);
echo $result;

我尝试使用 Nodejs 连接到服务器,但我无法使用 http 请求发布数据。 我总是收到以下错误:

"登录状态码:200
警告:implode():在 /www/wwwroot/ldtapi.plgfutar.hu/dashboard.php316 行 中传递的参数无效>

警告/www/wwwroot/ldtapi.plgfutar.hu/dashboard.php 中的 foreach() 提供的参数无效321
{"result":"success","event":"getawb","msg":[]}"

我的代码如下:

function getAwb() {         
  
  const url = `${protocol}//${host}${getawbpath}`

  const data = JSON.stringify({
    order_ids: '97621'
  });

  const options = {              
    mehod: 'POST',                                             
    auth: username,
    CURLOPT_POSTFIELDS: data
  };

  const req = http.request(url, options, (res) => {               
    console.log('Login statusCode:', res.statusCode);
          
    res.on('data', (d) => {                       
        process.stdout.write(d);
      });

    res.on("error", (err) => {
      reject(err);
    });
  });                                           

  req.write(data);

  req.on('error', (e) => {
  console.error(e);
  });

  req.end();

getAwb();

我的代码有什么问题?我应该如何将数据发布到服务器以获得所需的响应?

【问题讨论】:

  • curl 与这些错误有什么关系?他们对 PHP 代码的问题非常具体,甚至给你一个行号来查看。至于JS,为什么你认为CURLOPT_POSTFIELDS是这个方法参数的有效属性名?

标签: php node.js request http-post


【解决方案1】:

我看到的问题:

  • 你是如何结束混合 PHP 和 nodejs 选项的? (CURLOPT_POSTFIELDS 是一个 php 选项) 您需要阅读文档以找到合适的选项。 (见下面的例子)

  • http_build_query 返回一个查询字符串。在您的 nodejs 示例中,您将 json 有效负载发送到不同的服务器。如果这是服务器所期望的,您需要生成一个查询字符串。

// query string generation in php
$data = array(
    'foo' => 'bar',
    'baz' => 'boom',
);
echo http_build_query($data) . "\n";
// foo=bar&baz=boom
$curl_parameters['order_ids'] = array(12);
http_build_query($curl_parameters);
// shows : order_ids%5B0%5D=12

从这里我得到这个应该可以工作的示例代码 (https://nodejs.dev/learn/making-http-requests-with-nodejs) 更多信息:https://usefulangle.com/post/167/nodejs-post-request

const https = require('https')

const data = 'order_ids%5B0%5D=12'

const options = {
  hostname: host,
  auth: username
  port: 443,
  path: getawbpath,
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', d => {
    process.stdout.write(d)
  })
})

req.on('error', error => {
  console.error(error)
})

req.write(data)
req.end()

【讨论】:

  • 感谢您的回复。我尝试了你的建议,但直到得到相同的错误代码。
  • 我可以在第 316 行查看 php 的相关行吗:/www/wwwroot/ldtapi.plgfutar.hu/dashboard.php
  • 当然可以,但是如何保存呢?我的 js 文件包含 140 行。同时,也许我发现了问题,但仍然无法解决。因此,PHP 示例请求一个数组,然后使用 http_build_query 对其进行转换。如何在节点中解决它?现在我知道数组在 JS 中看起来不同,基本上 PHP 数组在 JS 中看起来像和对象。但是服务器仍然无法理解发送的对象。
  • 我编辑了我的答案。看起来你没有推送正确的数据。它不像您的 nodejs 代码中的 { order_ids: '97621' }。它是 { order_ids: ['97621'] } AND querystringified : order_ids[0]=97621
  • 我尝试发送所有变体但仍然失败。'order_ids[0]=97621' and 'order_ids%5B0%5D=97621' and { order_ids: ['97621'] } and [ 'order_ids[0]=97621']。在最后两个变体的情况下,我得到了一个新的错误 ID,即“ERR_INVALID_ARG_TYPE”。任何进一步的想法如何解决它?
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 2015-02-03
  • 1970-01-01
相关资源
最近更新 更多