You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Glue Python作业SOAP请求返回XML错误求助

解决AWS Glue中SOAP请求XML声明前置空格问题

作为AWS Glue新手,我编写了Python脚本在Glue中发送SOAP请求并将响应存储到S3。作业运行成功,但收到XML错误响应,相同代码在PyCharm中运行正常。

错误XML响应

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Receiver</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">Server was unable to process request. ---> Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 10.</soap:Text>
</soap:Reason>
<soap:Detail/>
</soap:Fault>
</soap:Body>
</soap:Envelope>

Glue作业脚本

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
import requests
import boto3

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

print("Imported Libraries")

url = "https://www.w3schools.com/xml/tempconvert.asmx"

data ="""
       <?xml version="1.0" encoding="utf-8"?>
        <soap12:Envelope
                xmlns:xsi="http://w3.org/2002/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:soap12="http://schemas.xmlsoap.org/soap/envelope/">
        <soap12:Body>
          <CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/">
          <Celsius>20</Celsius>
          </CelsiusToFahrenheit>
        </soap12:Body>
       </soap12:Envelope>"""
headers = {
    'Content-Type': 'text/xml; charset=utf-8'
}
response = requests.request("POST", url, headers=headers, data=data)

var = response.text
print(f"Response: {var}")

client = boto3.client('s3')
client.put_object(Body=var, Bucket='my-bucket', Key='data/soap_inbound.xml')

print("S3 object created")

job.commit()

问题原因

服务器报错明确指出XML声明必须是文档的第一个节点,其前不能有空白字符。你的SOAP请求数据字符串开头存在换行和缩进空格,导致实际发送的XML内容前有空白,触发了这个错误。在PyCharm中可能因为字符串处理的细微差异没有暴露这个问题,但Glue环境下这个空白被完整发送给了服务器。

修复方案

修改data变量的定义,确保XML声明是字符串的第一个字符,去掉开头的换行和多余缩进:

data ="""<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
        xmlns:xsi="http://w3.org/2002/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:soap12="http://schemas.xmlsoap.org/soap/envelope/">
<soap12:Body>
  <CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/">
  <Celsius>20</Celsius>
  </CelsiusToFahrenheit>
</soap12:Body>
</soap12:Envelope>"""

修改后重新运行Glue作业,服务器将正确解析SOAP请求,返回正常的温度转换结果。

内容的提问来源于stack exchange,提问作者marie20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:52:42