You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js发送含RefId的Form Data POST请求时RefId为空的解决方法

Fixing Node.js POST Request to Send RefId Correctly

Let's break down why your current Node.js code isn't sending the RefId properly to the target server, and fix it with working solutions.

What's Wrong With Your Current Code?

You have two key issues causing the server to receive a null RefId:

  1. Incorrect FormData Usage: You added an extra name: 'RefId' field, which is unnecessary. Your original frontend code only sends a single field named RefId with the value of refIdValue — your Node.js code was sending two fields instead.
  2. Wrong Content-Type Header: Setting Content-Type: 'text/javascript' tells the server you're sending JavaScript code, not form data. The server can't parse this format, so it can't extract the RefId.

Correct Solutions

There are two reliable ways to replicate your original frontend form submission behavior in Node.js:

Option 1: Use application/x-www-form-urlencoded (Matches Frontend Default)

This is the default content type for browser form submissions, so it's the closest match to your original JavaScript code. You can use either the qs library or Node.js built-in URLSearchParams:

Using qs Library

First install the library if you haven't:

npm install qs

Then implement the function:

const axios = require('axios');
const qs = require('qs');

function postRefId(refIdValue) {
  axios({
    method: 'post',
    url: 'https://bpm.shaparak.com',
    data: qs.stringify({ RefId: refIdValue }),
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
  .then(response => {
    console.log('postRefId success', response);
  })
  .catch(error => {
    console.log('postRefId error', error);
  });
}
Using Built-in URLSearchParams (No Extra Libraries)

If you're using Node.js 10 or later, you can use the native URLSearchParams without installing additional packages:

const axios = require('axios');

function postRefId(refIdValue) {
  const formData = new URLSearchParams();
  formData.append('RefId', refIdValue);

  axios.post('https://bpm.shaparak.com', formData)
  .then(response => {
    console.log('postRefId success', response);
  })
  .catch(error => {
    console.log('postRefId error', error);
  });
}

Option 2: Use multipart/form-data with FormData

If the server accepts multipart/form-data (though your frontend uses the other type, this is also valid), make sure to use the form-data package correctly and let axios handle the headers:

First install the form-data package:

npm install form-data

Then implement the function:

const axios = require('axios');
const FormData = require('form-data');

function postRefId(refIdValue) {
  const bodyFormData = new FormData();
  // Only append the RefId field — no extra fields needed
  bodyFormData.append('RefId', refIdValue);

  axios({
    method: 'post',
    url: 'https://bpm.shaparak.com',
    data: bodyFormData,
    // Let form-data generate the correct headers (including boundary)
    headers: bodyFormData.getHeaders()
  })
  .then(response => {
    console.log('postRefId success', response);
  })
  .catch(error => {
    console.log('postRefId error', error);
  });
}

Key Takeaways

  • Always match the content type the server expects (either application/x-www-form-urlencoded or multipart/form-data).
  • Don't add unnecessary fields to your form data — only send the RefId field as your original code does.
  • Let axios or the form-data package handle header generation when possible to avoid typos or incorrect values.

内容的提问来源于stack exchange,提问作者Fateme Ahmadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:29:57