JavaScript调用JIRA REST API无法创建问题求助
Hey Kavitha, sorry to hear you're stuck with this frustrating problem—let's break down the most likely reasons your curl command works but the JavaScript code isn't creating the issue, even without throwing visible errors.
Common Causes & Fixes
1. Missing or Incorrect Content-Type Header
JIRA's REST API requires the request body to be sent as application/json, but many JS HTTP libraries (like fetch or axios) don't set this header by default. Curl handles this automatically when you pass -H "Content-Type: application/json", but JS needs explicit configuration.
Fix for fetch:
fetch('https://your-jira-instance/rest/api/2/issue', { method: 'POST', headers: { 'Content-Type': 'application/json', // This line is critical! 'Authorization': 'Basic ' + btoa('your-username:your-password') }, body: JSON.stringify(yourIssuePayload) })
Fix for XMLHttpRequest:
const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://your-jira-instance/rest/api/2/issue'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequestHeader('Authorization', 'Basic ' + btoa('your-username:your-password')); xhr.send(JSON.stringify(yourIssuePayload));
2. Unserialized Request Body
If you're passing a plain JavaScript object directly as the request body (without JSON.stringify()), the API won't parse it correctly. Curl uses the raw JSON string, so make sure your JS code converts the object to a JSON string explicitly.
Bad:
body: yourIssuePayload // Passing the object directly
Good:
body: JSON.stringify(yourIssuePayload) // Serializing to JSON string
3. Silent HTTP Errors (Not Handling Response Status)
Many JS libraries like fetch only reject promises on network errors—HTTP errors (like 400 Bad Request, 403 Forbidden) still resolve. If you're not checking the response status, you'll miss JIRA's detailed error messages.
Add Error Handling to Reveal Issues:
fetch('https://your-jira-instance/rest/api/2/issue', { /* ... */ }) .then(response => { if (!response.ok) { // Get the error message from JIRA return response.text().then(errorText => { throw new Error(`Request failed: ${response.status} - ${errorText}`); }); } return response.json(); }) .then(data => console.log('Issue created:', data)) .catch(error => console.error('Detailed error:', error));
This will show you exactly what JIRA is complaining about—like missing required fields, invalid values, or permission issues that were hidden before.
4. CORS Restrictions (If Running in Browser)
If your JS code runs in a browser (not Node.js), cross-origin resource sharing (CORS) might be blocking the request silently. Curl doesn't have this restriction since it's a server-side tool.
Check:
- Open your browser's DevTools > Network tab
- Look for an
OPTIONSpreflight request—if it fails with a 403 or 405 status, CORS is the problem.
Fix:
- Ask your JIRA admin to configure the instance to allow your frontend domain in CORS settings
- Or use a proxy server to forward requests from your frontend to JIRA.
5. Authorization Header Discrepancies
Double-check that your Basic Auth header is formatted correctly:
- Combine your username and password with a colon:
username:password - Encode the string in Base64 (use
btoa()in browser, orBuffer.from('username:password').toString('base64')in Node.js) - Prefix with
Basic(note the space after "Basic")
Compare the encoded string to what curl uses—you can decode the curl auth header to verify consistency.
6. Hidden Differences in JSON Payload
Even if you think the payload is identical, JS might add extra whitespace or escape characters when stringifying. Print the serialized JSON from your code and compare it line-by-line with the curl payload:
console.log(JSON.stringify(yourIssuePayload, null, 2));
Look for subtle differences like missing quotes, extra commas, or incorrect field names.
Final Steps
Start with adding proper error handling to see JIRA's response—this will almost always point you to the root cause. Then verify the Content-Type header and payload serialization.
内容的提问来源于stack exchange,提问作者Kavitha Yeleti

