javascriptpythonrequestfastapijira-rest-api

Cannot resolve errors with Jira API v2


my frameworks:

using this fetch:

const response = await fetch("UrlToPythonBackend", {
            method: "POST",
            mode: "no-cors", // changing this option in my tries
            headers: {
                "Content-Type": "application/json",
                "X-Atlassian-Token": "no-check",
                "Target": "https://DomainWichWorks100%Fine.atlassian.net/rest/api/2/issue",
                Authorization: 'Basic MyTokenWichWorks100%FineTrustMe',
            },
            body: JSON.stringify({
                test: "Test"
            }),
            redirect: 'follow'
        })

using this backend:

from fastapi import FastAPI
from fastapi import Body, Header, Request, HTTPException
from typing import Annotated
import requests
import json
from fastapi_cors import CORS
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],  # or specify methods like ["GET", "POST"]
    allow_headers=["*"],  # or specify headers like ["Content-Type", "Authorization"]
)


@app.post("/proxy")
async def proxyPost(request: Request, body = Body(), target: str = Header(alias="Target")):
    """
    Proxies the POST request to the specified target URL.

    Args:
        request (Request): The incoming request object.
        body (dict): The request body as a dictionary.
        target (str): The target URL to proxy the request to.

    Returns:
        dict: The JSON response from the target URL.
    """
    print(f"Request Body: {body}")


    headers = {key: value for key, value in request.headers.items() if key.lower() != 'host'}


    load = str(body)
    load = load.replace("'", "\"")

    response = requests.post(target, json=load, headers=headers)
    try:
        response_json = response.json()
    except:
        response_json = response.text
    return response_json

    #response = requests.post(target, json=body, headers=headers)
    #response_json = response.json()

Short: getting the 422 Unprocessable Entity HTTPError

Detailed: I've tried to deploy the backend on a server but then i'm getting with cors the XSRF check error and with no-cors the Unprocessable Entity Error

Using the Backend Localhost (changed the mode of the fetch to cors), everything works fine except the jira api. On localhost i'm getting XSRF check failed even though im using the solution Jira provided: X-Atlassian-Token: no-check


Solution

  • solved my problem by adding these two headers in my backend now its working.

    headers['Accept'] = '/' headers['User-Agent'] = 'curl/7.64.1'