How to use Basic Authentication policy with service callout policy in apigee edge?

Hello Team,

I have to call from my backend URL using service callout policy for that URL, I have to send username and password using basic authentication. How to build basic authentication in servicecallout.

Thank you,

Solved Solved
1 2 1,494
2 ACCEPTED SOLUTIONS

Not applicable

In service callout you can set the header Authorization, in which you will send basic Authorization value.

Also you can use basic Authentication policy before service callout, and encode the username and password to another variable and then set the variable to Authorization header in the service callout.

<BasicAuthentication name="ApplyBasicAuthHeader"> 
  <DisplayName>ApplyBasicAuthHeader</DisplayName>   
<Operation>Encode</Operation>  
 <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>  
 <User ref="BasicAuth.credentials.username" />  
 <Password ref="BasicAuth.credentials.password" />  
 <AssignTo>xyz</AssignTo>
</BasicAuthentication>

Then

<ServiceCallout name="ServiceCallout-GeocodingRequest1">
    <DisplayName>Inline request message</DisplayName>
    <Request variable="authenticationRequest">
      <Set>
        <Headers>
          <Header name="Authorization">{xyz}</Header>
        </Headers>
      </Set>
    </Request>
    <Response>GeocodingResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
      <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

View solution in original post

Not applicable

You can try message template in service callout.

    <Set>
       <Headers>
         <Header name="request.header.Authorization">Basic {encodeBase64(request.header.username':'request.header.password)}</Header>
       </Headers>
    </Set>

View solution in original post

2 REPLIES 2

Not applicable

In service callout you can set the header Authorization, in which you will send basic Authorization value.

Also you can use basic Authentication policy before service callout, and encode the username and password to another variable and then set the variable to Authorization header in the service callout.

<BasicAuthentication name="ApplyBasicAuthHeader"> 
  <DisplayName>ApplyBasicAuthHeader</DisplayName>   
<Operation>Encode</Operation>  
 <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>  
 <User ref="BasicAuth.credentials.username" />  
 <Password ref="BasicAuth.credentials.password" />  
 <AssignTo>xyz</AssignTo>
</BasicAuthentication>

Then

<ServiceCallout name="ServiceCallout-GeocodingRequest1">
    <DisplayName>Inline request message</DisplayName>
    <Request variable="authenticationRequest">
      <Set>
        <Headers>
          <Header name="Authorization">{xyz}</Header>
        </Headers>
      </Set>
    </Request>
    <Response>GeocodingResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
      <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Not applicable

You can try message template in service callout.

    <Set>
       <Headers>
         <Header name="request.header.Authorization">Basic {encodeBase64(request.header.username':'request.header.password)}</Header>
       </Headers>
    </Set>