定义Request默认配置失效问题求助:仅body和url生效
It looks like you've set up default configurations for your request instance, but only the url and body from your options are being used—let's figure out why and fix this.
Common Causes & Fixes
1. Adjust how you call the pre-configured request instance
When using request-promise-native, the defaults() method returns a pre-configured request instance, but sometimes using HTTP method shorthands like .post() can lead to unexpected merging of defaults. Try modifying your createCommit function to explicitly define the method in your options and call the instance directly:
async function createCommit(commitRequestBody) { const options = { method: 'POST', url: `${GIT_API_URL}/projects/${config.PROJECT_ID}/repository/commits`, body: commitRequestBody, }; // Call the pre-configured instance directly to ensure defaults are merged const response = await gitLabDefaultRequest(options); return response; }
This ensures your default headers, json: true, timeout, and other settings are properly combined with your request-specific options.
2. Verify your config values aren't empty
Double-check that config.GIT_API_ACCESS_TOKEN isn't empty or undefined—if it is, the PRIVATE-TOKEN header won't be sent, which might make you think all defaults are failing. You can log the merged options to confirm:
async function createCommit(commitRequestBody) { const options = { method: 'POST', url: `${GIT_API_URL}/projects/${config.PROJECT_ID}/repository/commits`, body: commitRequestBody, }; // Log the final merged configuration to verify defaults are included console.log('Final request options:', gitLabDefaultRequest.defaults(options)); const response = await gitLabDefaultRequest(options); return response; }
3. Use baseUrl for cleaner, more reliable configuration
To avoid repeating the base API URL and reduce potential errors, move GIT_API_URL into your defaults as baseUrl:
const gitLabDefaultRequest = request.defaults({ baseUrl: GIT_API_URL, // Set base URL here headers: { 'Content-Type': 'application/json', 'PRIVATE-TOKEN': config.GIT_API_ACCESS_TOKEN, }, json: true, simple: true, timeout: 15000, resolveWithFullResponse: true, }); // Update createCommit to use a relative URL async function createCommit(commitRequestBody) { const options = { method: 'POST', url: `/projects/${config.PROJECT_ID}/repository/commits`, body: commitRequestBody, }; const response = await gitLabDefaultRequest(options); return response; }
4. Debug the actual request being sent
Enable request debugging to see exactly what headers and settings are being sent with your request. Add this line at the top of your file:
require('request').debug = true;
This will log detailed request information to your console, letting you confirm if your default headers, timeout, and other settings are actually being applied.
5. Upgrade request-promise-native to the latest version
Older versions of the library had bugs related to default configuration merging. Run this command to upgrade:
npm install request-promise-native@latest
内容的提问来源于stack exchange,提问作者David Faizulaev

