People much smarter than myself figured it out here:
https://autohotkey.com/board/topic/7152 ... eam/page-2. I vaguely modified it to call it more easily below.
To define the function:
Code: Select all
httpGetter(RequestType := ""
,URL := ""
,Payload := ""
,FullFilePath := ""
,OpenAfterSave := True ){
pWHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
pWHttp.Open(RequestType, URL, 0)
pWHttp.SetRequestHeader("Content-Type", "application/json")
if(StrLen(Payload) > 0)
pWHttp.Send(Payload)
else
pWHttp.Send()
pWHttp.WaitForResponse(5)
vStream := pWHttp.ResponseStream
if (ComObjType(vStream) = 0xD) { ;VT_UNKNOWN = 0xD
pIStream := ComObjQuery(vStream, "{0000000c-0000-0000-C000-000000000046}") ;defined in ObjIdl.h
oFile := FileOpen( FullFilePath, "w")
Loop {
VarSetCapacity(Buffer, 8192)
hResult := DllCall(NumGet(NumGet(pIStream + 0) + 3 * A_PtrSize) ; IStream::Read
, "ptr", pIStream
, "ptr", &Buffer ;pv [out] A pointer to the buffer which the stream data is read into.
, "uint", 8192 ;cb [in] The number of bytes of data to read from the stream object.
, "ptr*", cbRead) ;pcbRead [out] A pointer to a ULONG variable that receives the actual number of bytes read from the stream object.
oFile.RawWrite(&Buffer, cbRead)
} Until (cbRead = 0)
ObjRelease(pIStream)
oFile.Close()
if FileExist( FullFilePath )
{
IF(OpenAfterSave = True)
run % FullFilePath
}
else
msgbox File Not Exist
}
}
return
To call the function:
Code: Select all
httpGetter(RequestType := "POST"
,URL := "http://myaccount.pythonanywhere.com/generatePDF/"
,Payload := "[{""SKU"":""asdf"",""Serial"":""asdf""}]"
,FullFilePath := A_Desktop . "\myfile.pdf"
,OpenAfterSave := True )