SOLVED: Help with a HTTP POST request with a binary (PDF) response Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sergio
Posts: 45
Joined: 29 Sep 2013, 16:36

SOLVED: Help with a HTTP POST request with a binary (PDF) response

03 Mar 2017, 19:24

I have to call an API & collect the PDF response. After looking at a few options, it looks like Blackholyman's code in this link is the easiest.

However, the response provided by the server is a binary file (PDF) which I need to download onto my computer. I'm still googling but I'm only getting other options & not really a way to work with what I have & like. Does anyone know if there's a way to interpret/download a PDF as a result of this request?


Code below was not written by me.

Code: Select all

WinHTTP := ComObjCreate("WinHTTP.WinHttpRequest.5.1")
;~ WinHTTP.SetProxy(0)
WinHTTP.Open("POST", "https://www.getflix.com.au/api/v1/regions.json", 0)
WinHTTP.SetCredentials("<api_key>", "x", 0)
WinHTTP.SetRequestHeader("Content-Type", "application/json")
Body := "{'service':'netflix','region':'US'}"
WinHTTP.Send(Body)
Result := WinHTTP.ResponseText
Status := WinHTTP.Status

msgbox % "status: " status "`n`nresult: " result
Last edited by Sergio on 03 Mar 2017, 23:30, edited 1 time in total.
Sergio
Posts: 45
Joined: 29 Sep 2013, 16:36

Re: Help with a HTTP POST request with a binary (PDF) response  Topic is solved

03 Mar 2017, 23:28

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	)


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, Bing [Bot] and 356 guests