hadi nik
hadi nik

Reputation: 43

Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock

I'm having trouble accessing a custom header named X-Subject-Token in the response of an AJAX request using jQuery. Here's my code:

async function submitHandler(e) {
    e.preventDefault();
    let BASE_URL = "http://{my-ip}:3005"
    if (usernameInput.value && passwordInput.value) {

        let json = {
            name: usernameInput.value,
            password: passwordInput.value
        }

        console.log(json)

        try {
            let response = await fetch(${BASE_URL}/v1/auth/tokens, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(json)
            });

            if (response.ok) {
                let token = String(response.headers.get("X-Subject-Token"))
                console.log(token)
                response.headers.forEach(h=>{
                    console.log(h)
                })
            }
            let data = await response.json();
            console.log(data);
        } catch (err) {
            console.log(err)
        }
    }
}

I've verified that the X-Subject-Token header is present in the response using browser developer tools. However, when I try to access it using jqXHR.getResponseHeader('X-Subject-Token'), it returns only content type ,Content length and Cache control.

I've also checked for CORS issues, but the server appears to be configured correctly to allow the custom header.

keyrock:
image: quay.io/fiware/idm:${KEYROCK_VERSION}
container_name: fiware-keyrock
hostname: keyrock
networks:
  default:
    ipv4_address: 172.18.1.5
depends_on:
  - mysql-db
  - authzforce
ports:
  - "${KEYROCK_PORT}:${KEYROCK_PORT}" # localhost:3005
environment:
  - DEBUG=idm:*
  - IDM_DB_HOST=mysql-db
  - IDM_DB_PASS_FILE=/run/secrets/my_secret_data
  - IDM_DB_USER=root
  - IDM_HOST=http://localhost:${KEYROCK_PORT}
  - IDM_PORT=${KEYROCK_PORT}
  - IDM_HTTPS_PORT=${KEYROCK_HTTPS_PORT}
  - IDM_ADMIN_USER=alice
  - [email protected]
  - IDM_ADMIN_PASS=test
  - IDM_PDP_LEVEL=advanced
  - IDM_AUTHZFORCE_ENABLED=true
  - IDM_AUTHZFORCE_HOST=authzforce
  - IDM_AUTHZFORCE_PORT=${AUTHZFORCE_PORT}
  - IDM_CSP_FORM_ACTION=*
  - IDM_CORS_ENABLED=true
  - IDM_CORS_ORIGIN=*
  - IDM_CORS_METHODS=GET,HEAD,PUT,PATCH,POST,DELETE
  - IDM_CORS_ALLOWED_HEADERS=content-type,X-Auth-Token,Tenant-ID,Authorization,Fiware-Service,Fiware-ServicePath,NGSILD-Tenant,NGSILD-Path,X-Subject-Token
  - IDM_CORS_CREDENTIALS=true
  - IDM_CORS_PREFLIGHT=true
  - IDM_CORS_EXPOSED_HEADERS=content-type,X-Auth-Token,Tenant-ID,Authorization,Fiware-Service,Fiware-ServicePath,NGSILD-Tenant,NGSILD-Path,X-Subject-Token

Can anyone help me understand why I'm unable to access the X-Subject-Token header in the AJAX response using jQuery? Any insights or suggestions would be greatly appreciated.

Upvotes: 0

Views: 61

Answers (0)

Related Questions