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

使用JavaScript向Web API发送POST请求返回401错误的问题排查求助

Hey there! Let's break down your 401 error and fix that API request step by step.

1. First, Understand the 401 Error

A 401 "Unauthorized" response means the API doesn't recognize you as a valid, authorized user. This is almost always related to missing or incorrect authentication credentials, but we also need to make sure your request body matches what the API expects.

2. Check for Missing Authentication Headers

Looking at your code, your headers only include Content-Type: application/json—most partner APIs require an authentication token or API key to let you submit data.

For example, if the API uses Bearer tokens, you'd add this to your headers:

headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_PARTNER_API_TOKEN" // Replace with your actual token
},

Or if it uses an API key header (common for partner integrations):

headers: {
    "Content-Type": "application/json",
    "X-Partner-API-Key": "YOUR_API_KEY" // Check the API docs for the exact header name
},

You'll need to find this authentication detail in the Njuko partner API documentation—this is likely the root cause of your 401 error.

3. Fix Your Request Body to Match the API Example

Your current request is missing several fields that the API example shows are required (or at least expected):

  • methodName: The payment method label (e.g., "Partner payment (VISA)" from the example)
  • datas: An array of custom field values (each with value and a field ID)
  • metaData: Optional or required metadata (like the Strava activity ID in the example)

Here's an updated version of your fetch call with these fields included (fill in the placeholders with your actual data):

fetch("https://front-api.njuko.com/partner/registration", {
    method: "POST",
    body: JSON.stringify({
        edition: edition,
        competition: competition,
        price: price,
        firstname: firstname,
        lastname: lastname,
        email: email,
        // Add missing required fields from the API example
        methodName: "Partner payment (VISA)", // Adjust based on your payment method
        datas: [
            {
                value: "Your address or required custom value", // Replace with actual input
                field: "REPLACE_WITH_THE_CORRECT_FIELD_ID" // Get this from API docs or your form setup
            }
        ],
        metaData: [
            {
                key: "stravaActivityId",
                label: "strava-activity-id",
                value: "YOUR_STRAVA_ID_IF_REQUIRED" // Omit this if it's optional
            }
        ]
    }),
    headers: {
        "Content-Type": "application/json",
        // Add your authentication header here
        "Authorization": "Bearer YOUR_PARTNER_TOKEN"
    },
})
.then(function (response) {
    // Add error checking to get more details from the API
    if (!response.ok) {
        console.log("Response status:", response.status);
        return response.text().then(errorText => { throw new Error(errorText) });
    }
    return response.json();
})
.then(function (data) {
    console.log("Success!", data);
})
.catch(function (error) {
    console.error("Request failed:", error);
});
4. Quick Troubleshooting Tips
  • Verify ID values: The edition, competition, price, and field IDs in the example are MongoDB-style 24-character strings. Make sure your form inputs are passing these actual IDs (not just the human-readable names like "2024 Summer Edition").
  • Check API error details: The updated catch block will log raw error text from the API—this often tells you exactly what's missing (e.g., "Missing authentication token" or "Required field 'methodName' not provided").
  • Double-check the docs: Always refer back to the official Njuko partner API docs for exact authentication requirements and field specifications—this is the most reliable source of information.

内容的提问来源于stack exchange,提问作者Vincent Vargas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 01:17:35