对于我的Ruby on Rails项目,我将邮件gem与net/http/
和net/http/post/multipart
一起使用,以将多部分post http请求发送到外部API,以将MMS消息从Web应用程序发送到手机。
我的多部分http发布请求将包含一个XML部分(是一些XML),一个smil部分(可以为空或空),然后是一个图像部分(例如jpeg图像及其图像字符串)。
首先,对于XML部分,我使用以下命令设置解析器:
wsdl = Rails.root.join('app','services', 'wsdls','mm7.wsdl')
self.xml_parser = Savon.client(wsdl: wsdl.to_s,
convert_request_keys_to: :none,
log: true, logger: Rails.logger, namespace_identifier: nil)
请注意,我的wsdl
仅来自https://markmail.org/download.xqy?id=rnl72qvlptaeylfg&number=3,只是我将其另存为文件。
然后,为了生成多部分的http发布请求,我使用了以下代码:
xml= xml_parser.build_request(:submit_message, message: {
MM7Version: '6.8.0',
SenderIdentification: {
VASPID: Settings.mms.vasp_id,
VASID: Settings.mms.vas_id,
SenderAddress:{Number: 'Some number'}
}, Recipients:{To: {Number: 'Some other number'}}}, soap_header:{
'mm7:TransactionID': 'some uuid'}, message_tag: 'SubmitReq').body
uri = URI.parse('fakeurl')
client = Net::HTTP.new(uri.host, uri.port)
mail = Mail.new do
content_type 'multipart/related; type="text/xml"'
end
soap_part = Mail::Part.new do
content_type 'text/xml; charset="utf-8"'
body xml
end
other_part = Mail::Part.new do
content_type 'multipart/related; type="application/smil"'
end
smil_part = Mail::Part.new do
content_type 'application/smil; charset="utf-8"'
end
#Put the image file here
other_part.add_file('script/ruby.jpg')
other_part.add_part(smil_part)
mail.add_part(soap_part)
mail.add_part(other_part)
http_headers = {"Content-Type" => 'multipart/related'}
mail.header.fields.each do |field|
http_headers[field.name] = field.to_s
end
使用Wireshark,我可以看到我的请求看起来像:
Accept: */*
User-Agent: Ruby
Host: somehost.org
Content-Type: multipart/form-data; boundary=-----------RubyMultipartPost
Content-Length: 7829
Connection: close
----==_mimepart_5c7019f236d9e_399610f2f2024937
Content-Type: text/xml;
charset=utf-8
Content-Transfer-Encoding: 7bit
<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header><mm7:TransactionID>c24f95c4-e72b-4343-b680-2f6b1251a888</mm7:TransactionID></env:Header><env:Body><SubmitReq><MM7Version>6.8.0</MM7Version><SenderIdentification><VASPID>XXX-qa</VASPID><VASID>XXX-qa</VASID><SenderAddress><Number>Some number</Number></SenderAddress></SenderIdentification><Recipients><To><Number>Some other number</Number></To></Recipients></SubmitReq></env:Body></env:Envelope>
----==_mimepart_5c7019f236d9e_399610f2f2024937
Content-Type: multipart/related;
boundary="--==_mimepart_5c7019f236cb5_399610f2f2024895";
charset=UTF-8;
type="application/smil"
Content-Transfer-Encoding: 7bit
----==_mimepart_5c7019f236cb5_399610f2f2024895
Content-Type: image/jpeg;
charset=UTF-8;
filename=ruby.jpg
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=ruby.jpg
Content-ID: <5c7019f2362d3_399610f2f2024785@dispatch-local.mail>
*******Here is the big image string*******
发送请求后,我得到的响应是:
HTTP Status 400 - Error unmarshalling envelope: javax.mail.MessagingException: Missing start boundary
我对应该如何处理起始边界感到困惑。在生成的请求中,我看到诸如boundary=-----------RubyMultipartPost
,----==_mimepart_5c7019f236d9e_399610f2f2024937
之类的东西,难道不是应该作为界限吗?
谢谢!