Search code examples
angularjsrestapigee

Getting error 401 response for requests to an Apigee API


I am trying to make requests to an Apigee API from an Angular service. It works with Postman, but I am getting 401 unauthorized error when making requests to the API from my Angular code.

Here is the Angular code I am trying:

$http({
       url: "https://xxx_test.com/data?sdate=date",
       method: "GET",
       headers: {
               'apikey': 'XXXXXXXXXXXXXXXXX'                             
             },
     })

This is the response that I am getting :

{"fault":{"faultstring":"Failed to resolve API Key variable request.header.apikey","detail":{"errorcode":"steps.oauth.v2.FailedToResolveAPIKey"}}}


Solution

  • Problem was on server side configuration. We are using apigee for our API so it needs some configuration for CORS.

    Here is settings :

    <FaultResponse>
            <Set>
                <StatusCode>200</StatusCode>
                <ReasonPhrase>CORS OK</ReasonPhrase>
                <Headers>
                    <Header name="Access-Control-Allow-Methods">GET, POST, PUT, DELETE, PATCH</Header>
                    <Header name="Access-Control-Allow-Origin">*</Header>
                    <Header name="Access-Control-Allow-Headers">authorization,content-type,api-key</Header>
                </Headers>
                <Payload/>
                <!-- Remove default fault content. -->
            </Set>
        </FaultResponse>
    

    Add RaisFault policy in your flow with condition.

    <Condition>request.verb == "OPTIONS" AND request.header.origin != null AND request.header.Access-Control-Request-Method != null</Condition>