Moxom160

Problem with SOAP with Attachment

15 posts in this topic

#1 ·  Posted (edited)

Hello

First i want to say that most of my problems i can solve while scripting by looking at the forum or in the help file but im stuck on this specific problem. I have been writing scripts to consume Web services and this works fine when its just plain XML responses. I work on a project now where i need to consume a SOAP response with an Attachment. I can make the plain SOAP call and get a response as well but i dont know how to handle the multipart and binary data. I have tried ADODB.Stream to write the data to a file but this just writes everything. I would be very grateful if someone could point me into the right direction.

Please find below the Request and the Response i get at the momen

 

$objHTTP = ObjCreate("Microsoft.XMLHTTP")
$objReturn = ObjCreate("Msxml2.DOMDocument.3.0")
$oDATA=ObjCreate("ADODB.Stream")

$strEnvelope = '<?xml version="1.0" encoding="UTF-8"?>' _
'<soapenv:Envelope' _
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' _
'xmlns:xsd="http://www.w3.org/2001/XMLSchema"' _
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' _
'<soapenv:Body>' _
'<ns1:getStorageAccessTicket' _
'soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"' _
'xmlns:ns1="NBRStorageService">' _
'<siteId xsi:type="xsd:long">123456</siteId>' _
'<username xsi:type="xsd:string">USERNAME</username>' _
'<password xsi:type="xsd:string">PASSWORD</password>' _
'</ns1:getStorageAccessTicket>' _
'</soapenv:Body>'
'</soapenv:Envelope>' _

; Set up to post to the server
$objHTTP.open ("post", "API end point" , False)

; Set a standard SOAP/ XML header for the content-type
$objHTTP.setRequestHeader ("Content-Type", "application/xml")

; Set a header for the method to be called
$objHTTP.setRequestHeader ("SOAPAction", 'getStorageAccessTicket')

ConsoleWrite("Content of the Soap envelope :" & @CR & $strEnvelope & @CR & @CR )

; Make the SOAP call
$objHTTP.send ($strEnvelope)

; Get the return envelope
$strReturn = $objHTTP.responsetext

With $oDATA
        .Type=1
        .Open
        .Write($objHTTP.ResponseBody)
        .SaveToFile(@ScriptDir&'\captcha.arf', 2)
        .Close
    EndWith
EndFunc

RESPONSE:
'------=_Part_5_987625235.1444159091234'
'Content-Type: text/xml; charset=UTF-8'
'Content-Transfer-Encoding: binary'
'Content-Id: <DF72463F6FAFE80B930059E761DF7225>'

'<?xml version="1.0" encoding="UTF-8"?>'
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
 '<soapenv:Body>'
  '<ns1:downloadNBRStorageFileResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="NBRStorageService">'
   '<ns1:downloadNBRStorageFileReturn xsi:type="soapenc:Array" soapenc:arrayType="ns1:DataHandler[2]" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">'
    '<item href="cid:FE782065C11E6558C9C8925EBE3D687F"/>'
    '<item href="cid:3CF0B37E4C5932A3E60EBA5226CFC30A"/>'
   '</ns1:downloadNBRStorageFileReturn>'
  '</ns1:downloadNBRStorageFileResponse>'
 '</soapenv:Body>'
'</soapenv:Envelope>'
------=_Part_5_987625235.1444159091234
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <FE782065C11E6558C9C8925EBE3D687F>

Attachment.arf
2342371
false
------=_Part_5_987625235.1444159091234
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <3CF0B37E4C5932A3E60EBA5226CFC30A>

 

Edited by Moxom160
correction

Share this post


Link to post
Share on other sites



Moxom160,

.SaveToFile(@ScriptDir&'\captcha.arf', 2)

As the Forum rules are clear on the fact that we do not support anything that deals with CAPTCHAs, could you please explain what this line is doing.

M23

 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

Hello Melba

I think there is missunderstanding. I was trying around different bits of code and found one snipped where the file name was set to captcha.png. since i was only testing i did not change the file name to something else. Im not planning on dealing with CAPTCHAs or anything like this.

To prove my indention please find below a link to the service im trying to access. Im trying to retrieve webex recordings via the NBR(network based recording) API.

https://developer.cisco.com/site/webex-developer/develop-test/nbr-web-services-api/api-functions.gsp#downloadNBRStorageFile

Sorry for the missunderstanding.

regards

Share this post


Link to post
Share on other sites

Moxom160,

Fine - but please be careful which "snippets" you use in future.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

You need to parse returned data to extract the attachment. It shouldn't be hard since it's standard MIME.

Can you post the exact response? Parts are missing from your post.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Hello trancexx

The response i posted above is every thing i get in human readable form .after the last ------=_Part_5_987625235.1444159091234 and the content ids it streams down binary data.

I have tested today to stream the reponse via ADODB.Stream to a local file and this works fine. The only think im stuck at on how to seperate the binary data from the top 30 lines of response. Im only interested in the binary which i require to generate the recording file.

I have attached a response so you can have look at it. Thank you already for looking at this.

test.xml

Share this post


Link to post
Share on other sites

That file misses the most important data to process it correctly - the original MIME header. 


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Does this help further? I have this from the API doc from Cisco.

Soap Response:

  HTTP/1.1 200 OK
  Date: Wed, 28 Apr 2010 10:36:46 GMT
  Content-Type: multipart/related; type="text/xml";
  start="<92AE6C82DBBD12B997D03C502A058511>
    "; boundary="----=_Part_0_23877553.1272451006846"
    Connection: Close
    ------=_Part_0_23877553.1272451006846
    Content-Type: text/xml; charset=UTF-8
    Content-Transfer-Encoding: binary
    Content-Id: <92AE6C82DBBD12B997D03C502A058511>
  <?xml version="1.0" encoding="utf-8"?>
  <soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
      <ns1:downloadNBRStorageFileResponse
  soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:ns1="NBRStorageService">
        <downloadNBRStorageFileReturn
  soapenc:arrayType="ns1:DataHandler[2]" xsi:type="soapenc:Array"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
          <downloadNBRStorageFileReturn
  href="cid:B0A8EBE4A6E3C12D01C9AA96201968E5"
  xsi:type="ns1:DataHandler"/>
            <downloadNBRStorageFileReturn
  href="cid:6E5E9F315D7E46BF51A3729E71BD10D3"
  xsi:type="ns1:DataHandler"/>
          </downloadNBRStorageFileReturn>
        </ns1:downloadNBRStorageFileResponse>
      </soapenv:Body>
    </soapenv:Envelope>
  ------=_Part_0_23877553.1272451006846
  Content-Type: application/octet-stream
  Content-Transfer-Encoding: binary
  Content-Id: <B0A8EBE4A6E3C12D01C9AA96201968E5>
    a.arf
    188607
    ------=_Part_0_23877553.1272451006846
    Content-Type: application/octet-stream
    Content-Transfer-Encoding: binary
    Content-Id: <6E5E9F315D7E46BF51A3729E71BD10D3>
  .............
    

Share this post


Link to post
Share on other sites

Does this work?

$sFile = "test.xml"
$sCID = "16F8B262D5B9C561D8899E638D3B3D8C"
$sOutFile = "YourAttachment.txt"

GetCIDAttachment($sFile, $sCID, $sOutFile)

If @error Then
    $sErrDesc = "You can't use this method because "
    Switch @error
        Case 1
            $sErrDesc &= "it sucks for you."
        Case 2
            $sErrDesc &= "getting stream failed."
        Case 3
            $sErrDesc &= "loading EML failed."
        Case 4
            $sErrDesc = "Saving to file failed."
        Case 5
            $sErrDesc = "No such attachment exists."
    EndSwitch
    MsgBox(4096, "Error", $sErrDesc & @CRLF)
Else
    MsgBox(4096, "Yay!", "Attachment with Content-Id <" & $sCID & '> extracted to "' & $sOutFile & '"' & @CRLF)
EndIf





Func GetCIDAttachment($sPath, $sCID, $sOutFile = Null)
    Local $oErrHandler = ObjEvent("AutoIt.Error", GetAttachmentSize) ; Red Jon fucked up COM

    Local $oMessage = ObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
    If @error Then Return SetError(1, 0, 0)

    Local $oMessageStream = $oMessage.GetStream()
    If @error Then Return SetError(2, 0, 0)

    $oMessageStream.LoadFromFile($sPath)
    If @error Then Return SetError(3, 0, 0)

    $oMessageStream.Flush() ; this is essential

    For $oBodyPart In $oMessage.BodyPart.BodyParts
        For $oField In $oBodyPart.Fields
            If $oField.Name = "urn:schemas:mailheader:content-id" And $oField.Value = "<" & $sCID & ">" Then
                If $sOutFile Then Return $oBodyPart.GetDecodedContentStream().SaveToFile($sOutFile, 2) ; adSaveCreateOverWrite
                If @error Then Return SetError(4, 0, 0)
                Return $oBodyPart.GetDecodedContentStream().Read()
            EndIf
        Next
    Next

    Return SetError(5, 0, 0)
EndFunc

It should extract "attachment" with Content-Id <16F8B262D5B9C561D8899E638D3B3D8C> from "test.xml" to file named "YourAttachment.txt". All located in the script's folder.

1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Hello trancexx

thanks for the script but running the above on my XML gives me "no such attachment exists".  I have tried different Content-ID's to see if i get different result but its always the same error.

 

 

Edited by Moxom160

Share this post


Link to post
Share on other sites

Hello trancexx

thanks for the script but running the above on my XML gives me "no such attachment exists".  I have tried different Content-ID's to see if i get different result but its always the same error.

 

 

I told you that the file you posted here isn't full. It's missing header part where boundary is defined.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Something like this:

Content-Type: Multipart/Related; boundary=----=_Part_39_1838099744.1444244895679; type=text/xml;

 


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

My guess is that you're missing headers. For your original code it could be:

With $oDATA
    .Type = 2
    .Open()
    .WriteText($objHTTP.getAllResponseHeaders() & $objHTTP.ResponseText)
    .SaveToFile(@ScriptDir & '\captcha.arf', 2)
    .Close()
EndWith

 


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Hello trancexx

I have just tried you suggestions and you were absolutly right i need to get the full headers and formating corrected. Its working now perfectly. thank you for the support.

Have day.

mox

Share this post


Link to post
Share on other sites

@trancexx

As always, again I learned something interesting, as I wast thought that IMessage is only for EML , now I re read MSDN doc for this.
Thanks
 

mLipok


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-12-23
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • jandings
      By jandings
      Hello there,
      I'm trying to query a REST API of a webservice, SHOPWARE to be precise. The API is very well documented (https://developers.shopware.com/developers-guide/rest-api/#using-the-rest-api-in-your-own-a) but only using PHP. I tried some stuff but could not make it work.
      It should be very simple, because the following simply works in a Chrome-Browser:
      https://USERNAME:PASSPHRASE@www.SHOPWAREDOMAIN.com/api/orders/300
      queries me for username and passphrase and then dumps order number 300.
      Now I try to do the same in AutoIt (based on the wonderful work of many contributors here):
      #include-once #include "..\INCLUDE\winhttp.au3" #include "..\INCLUDE\OO_JSON.au3" ;Proxy Config Global $oJSON = _OO_JSON_Init() Global $obj = "" Global $sUserName = "USERNAME" Global $sPassword = "PASSPHRASE" Global $sDomain = "www.SHOPWAREDOMAIN.com/api/orders/300" Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) JOC/0.1") ; without ie proxy settings $hConnect = _WinHttpConnect($hOpen, "https://" & $sDomain) ConsoleWrite ($hConnect & "#") $sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", $sUserName & ":" & $sPassword & "@" & $sDomain, Default ) ;$sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", $sDomain, Default, $sUserName & ":" & $sPassword) ;$sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", $sDomain, Default, , $sUserName & ":" & $sPassword ) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ; See what's returned ConsoleWrite ($sReturned & "#" & @error) Whatever version I use (I tried the commented ones and many others), I always get a reply from Shopware:
      {"success":false,"message":"Invalid or missing auth"}
      At least this comes from Shopware. But there must be some format of the credentials that I don't know or understand.
      Any help?
    • tremolux66
      By tremolux66
      I've run into the problem where multiple script processes writing to a log file at the same time can interfere with one another. That is, file writes using FileWriteLine are not atomic, so a line of text from one process can have fragments of text from other processes mixed in with it. Exclusive file-locking should take care of the problem.
      Looking through the standard UDFs, I see that _WinAPI_LockFile is available, but according to the MSDN documentation it doesn't block - it returns immediately if the lock can't be obtained - and there doesn't seem to be a UDF for the LockFileEx function. I considered writing my own UDF for LockFileEx, but some aspects (e.g., creating the OVERLAPPED structure and its members) look too complex for the amount of time I have available. I think approximating LockFileEx using LockFile could solve my problem, but I'd like to keep it simple while still working reliably. I'm not confident that (e.g.) a simple retry loop to simulate blocking will be adequate.
      Any suggestions for a simple way to achieve exclusive file-locking with blocking?
    • AutID
      By AutID
      Hello,

      I will put it as simple as possible
      Why this code runs perfectly on 32bit and it fails on 64bit?
      Local $hWND = WinGetProcess("[CLASS:LSS_app]") ConsoleWrite($hWND & @LF) Local $hModuleList = _WinAPI_EnumProcessModules($hWND) If @error Then ConsoleWrite("Error: " & @error & @LF) For $i = 0 To $hModuleList[0][0] - 1 ;~ If StringInStr($hModuleList[$i][1], "sysCap64.dll") Then ConsoleWrite($hModuleList[$i][0] & @LF) ;~ EndIf Next As the title says EnumProcessModules returns error 10 which I have no clue what it is.
      It must be something with autoit or my lack of coding because a similar code in C# will work like a charm on both x86 and x64
      Process[] Processes = Process.GetProcessesByName("winLSS64Cap"); Process nProcess = Processes[0]; Handle = OpenProcess(0x10, true, (uint)nProcess.Id); for(int i = 0; i < nProcess.Modules.Count; i++) { Console.WriteLine(nProcess.Modules[i].ModuleName); }  
    • Progh0st
      By Progh0st
      I'm trying to use Sendspace API for download and upload, until now i only have learned to get token, login and get session key. My next step is upload a file. But i have no idea how to do this part.
      ; try to upload something ; Specify the reguest to upload.getInfo: Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "/rest/?method=upload.getinfo&session_key=" & $SessonKey & "&speed_limit=0") ; Send request Local $handle = _WinHttpSendRequest($hRequest) ; Wait for the response Local $WaitRS = _WinHttpReceiveResponse($hRequest) ; Read Data Local $sData = _WinHttpReadData($hRequest) MsgBox(0, "Upload Data", $sData) _filewrite($sData) ; Get all info needed from $sData $URL = _StringBetween($sData, "<upload url=", "progress") $sURL = $URL[0] $MaxFileSize = _StringBetween($sData, "max_file_size=", "progress") $sMaxFileSize = $MaxFileSize[0] $Upload_Identifier = _StringBetween($sData, "upload_identifier=", "&amp") $sUpload_Identifier = $Upload_Identifier[0] $ExtraInfo = _StringBetween($sData, "extra_info=", "/>") $sExtraInfo = $ExtraInfo[0] ;All well done until here ;Fill in form $XML = '<form method="post" action="' & $sURL & 'enctype="multipart/form-data">' & @CRLF $XML &= '<input type="hidden" name="MAX_FILE_SIZE" value=' & $sMaxFileSize & '>' & @CRLF $XML &= '<input type="hidden" name="UPLOAD_IDENTIFIER" value=' & $sUpload_Identifier & '>' & @CRLF $XML &= '<input type="hidden" name="extra_info" value=' & $sExtraInfo & '>' & @CRLF $XML &= '<input type="file" name=' & "C:\Users\Arlen\Desktop\premisa mrotal.txt" & '>' & @CRLF $XML &= '</form>' MsgBox(0,"", $XML) ; Now how do i send it? Is it correct? _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen)  
      sendspace_api_guide.pdf
    • Progh0st
      By Progh0st
      Hi all, i need help finding a good (guide, tutorial) on how to use Autoit with API. I would like to try Gmail API and DropBox API. But i have no idea where to start..
      Edit:
      I have learn a little bit, i know how to login to DepositFiles, But i have no idea how to upload and get the link from depositfiles any help? =)
      #include "WinHTTP.au3" $hSesson = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0") $Username = "user" $Password = "pass" $sHost = "depositfiles.org" $sData = "login=" & $Username & "&password=" & $Password & "&recaptcha_challenge_field=&recaptcha_response_field=&g-recaptcha-response=" $sHostC = _WinHttpConnect($hSesson, $sHost) $sPacket = _WinHttpSimpleRequest($sHostC, "POST", "/api/user/login", "http://depositfiles.org/login.php?return=%2F", $sData) ;_filewrite($sPacket)