Node.js发送含RefId的Form Data POST请求时RefId为空的解决方法
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:
- Incorrect FormData Usage: You added an extra
name: 'RefId'field, which is unnecessary. Your original frontend code only sends a single field namedRefIdwith the value ofrefIdValue— your Node.js code was sending two fields instead. - 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-urlencodedormultipart/form-data). - Don't add unnecessary fields to your form data — only send the
RefIdfield 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

