简体   繁体   中英

REST API Basic Auth is failing in AWS Lambda function

I am calling REST API Post method using AWS Lambda function. But the basic authentication failed. same code worked fine on pycharm. Could anyone pleasehelp.

Please find below sample code

***import json
import os
import sys
import logging
import argparse
import re
import requests
import pandas as pd
import boto3
import datetime
import time
import json
import base64
import xml.etree.ElementTree as ET
def lambda_handler(event, context):  
    head = {
            'Content-Type': 'application/xml',
            'Authorization': 'c2Vn6Z1oxK3R3Q18='
        }
    body = f"""<?xml version='1.0' encoding='UTF-8' ?>
            <ServiceRequest>
            <filters>           
                </filters>
             <preferences>
             <startFromOffset>1</startFromOffset>    
             <limitResults>1</limitResults>
              </preferences>
            </ServiceRequest>"""
            
    request_URL='https:'
    
    response = requests.post(request_URL, headers=head, data=body)
    print(response.text) 
                           
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }***

Getting below output

<?xml version="1.0" encoding="UTF-8"?>
<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https:">
<responseCode>INVALID_CREDENTIALS</responseCode>
<responseErrorDetails>
<errorMessage>HTTP BASIC AUTH header is malformed.</errorMessage>
</responseErrorDetails>
</ServiceResponse>

Same code gives xml response as output when i executed on Pycharm.

Your Authorisation header should be something like

'Authorization': 'Basic c2Vn6Z1oxK3R3Q18='

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM