英文:
Call soap from aws lambda in C#
问题
我对无服务器 Lambda 函数还不太熟悉,但是我可以帮你翻译一下你的问题。
你想知道如何从 Lambda 函数中调用一个使用 C# 编写的简单 SOAP API。在 Visual Studio 中,你可以添加引用,但是在 Lambda 中应该怎么做呢?我无法找到相关选项。
我尝试过添加引用,但没有成功。
英文:
I’m new on serverless lambda functions and I have deployed a simple soap api by using C# and I want to know how to call it from a lambda function.
In VS you add the reference but what about lambda? I can’t figure out the option
I’ve tried to add the reference with no luck
答案1
得分: 1
没有预先构建的方法可以使用Lambda函数与SOAP服务进行交互。您需要通过创建XML输入,通过HTTP请求发送它,然后解析响应来手动处理它。
以下是使用Python的示例,但您可以在Lambda支持的任何编程语言中实现此功能:
import json
import requests
from datetime import datetime, timedelta
from xml.dom import minidom
import os
url = os.environ['SERVICE_URL']
query_service_template = """
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SimpleQuery xmlns="http://example.ws.localhost/">
<m_field>%s</m_field>
</SimpleQuery>
</soap:Body>
</soap:Envelope>
"""
def build_body(field):
body = query_service_template % (field)
body = body.replace("\n", "")
return body.encode('utf-8')
def build_headers(request):
return {
"Content-Type": "text/xml; charset=utf-8",
"Content-Length": str(len(request))
}
def extract_data_from_xml(response_body):
xmldoc = minidom.parseString(response_body)
outputField = xmldoc.getElementsByTagName('outputField').item(0).firstChild
return {
'outputField': outputField,
}
def lambda_handler(event, context):
# Build request
request = build_body(event['input_field'])
# Build session
session = requests.session()
session.headers = build_headers(request)
# Post request
response = session.post(url=url, data=request, verify=True)
# Extract data from response
data = extract_data_from_xml(str(response.content, "UTF-8"))
return data
英文:
There isn't a pre-scaffolded method to interact with a SOAP service using a Lambda function.
You'd need to handle it "manually" by creating the XML input, sending it through an HTTP request, and then parsing the response.
Here's a sample using Python, but you can achieve this in any programming language that Lambda supports:
import json
import requests
from datetime import datetime, timedelta
from xml.dom import minidom
import os
url = os.environ['SERVICE_URL']
query_service_template = """
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
\t<soap:Body>
\t\t<SimpleQuery xmlns="http://example.ws.localhost/">
\t\t\t<m_field>%s</m_field>
\t\t</SimpleQuery>
\t</soap:Body>
</soap:Envelope>
"""
def build_body(field):
body = query_service_template % (field)
body = body.replace("\n", "")
return body.encode('utf-8')
def build_headers(request):
return {
"Content-Type": "text/xml; charset=utf-8",
"Content-Length": str(len(request))
}
def extract_data_from_xml(response_body):
xmldoc = minidom.parseString(response_body)
outputField = xmldoc.getElementsByTagName('outputField').item(0).firstChild
return {
'outputField': outputField,
}
def lambda_handler(event, context):
# Build request
request = build_body(event['input_field'])
# Build session
session = requests.session()
session.headers = build_headers(request)
# Post request
response = session.post(url=url, data=request, verify=True)
# Extract data from response
data = extract_data_from_xml(str(response.content, "UTF-8"))
return data
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论