WinHttpRequest, Ragic, Uploading files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

WinHttpRequest, Ragic, Uploading files

26 Aug 2019, 06:06

The usual working code to upload text to a field in ragic via AHK:

Code: Select all

Ragic(){

API_Key := "insert key here"

var1 = one
var2 = two
var3 = three
var4 = four
var5 = five
var6 = six

Data = 1000036=%var1%&1000037=%var2%&1000038=%var3%&1000039=%var4%&1000040=%var5%&1000041=%var6%&api=

WinHttp := ComObjCreate("MSXML2.XMLHTTP.6.0")
WinHttp.Open("POST", "TheURLlinkHere", false)
WinHttp.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
WinHttp.SetRequestHeader("Authorization", "Basic " API_Key)
WinHttp.Send(Data)
}

The Ragic documentation states that to upload a file you need to use the following cURL formatting;

Code: Select all

curl -F "1000088=@/your/file/path" \

-F 'api=' \

-F 'v=3' \

-H "Authorization:Basic YOUR_API_KEY_GOES_HERE" \

https://www.ragic.com/demo/sales/1
Can anyone help translate into the above code?
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: WinHttpRequest, Ragic, Uploading files

26 Aug 2019, 08:22

Downloaded the files in the link but they didn't seem to work. Will check again soon.
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: WinHttpRequest, Ragic, Uploading files

26 Aug 2019, 09:10

Yeah, basically downloaded CreateFormData.ahk and Example.ahk. Ran Example but had error that function did not exist, so I added #Include CreateFormData.ahk but again another error so I added #Include BinArr.ahk as well but still nothing.


Specifically: LoadFromFile

---> oADO.LoadFromFile(FileName)
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest, Ragic, Uploading files

26 Aug 2019, 09:27

Specifically: LoadFromFile

---> oADO.LoadFromFile(FileName)
I'm guessing you haven't modify the file path to an existing file on your computer.

You can use the modified version by SKAN that doesn't require BinArr. I've included the code in below. Just remember to change "D:\your\file\path", "TheURLlinkHere", API_Key before running the script.

Code: Select all

objParam := { "api": ""
             , "v": "3"
             , "1000088": ["D:\your\file\path"] }
CreateFormData(Data, hdr_ContentType, objParam)

WinHttp := ComObjCreate("MSXML2.XMLHTTP.6.0")
WinHttp.Open("POST", "TheURLlinkHere", false)
WinHttp.SetRequestHeader("Content-Type", hdr_ContentType)
WinHttp.SetRequestHeader("Authorization", "Basic " API_Key)
WinHttp.Send(Data)

MsgBox % WinHttp.ResponseText

; CreateFormData() by tmplinshi, AHK Topic: https://autohotkey.com/boards/viewtopic.php?t=7647
; Thanks to Coco: https://autohotkey.com/boards/viewtopic.php?p=41731#p41731
; Modified version by SKAN, 09/May/2016

CreateFormData(ByRef retData, ByRef retHeader, objParam) {
	New CreateFormData(retData, retHeader, objParam)
}

Class CreateFormData {

	__New(ByRef retData, ByRef retHeader, objParam) {

		Local CRLF := "`r`n", i, k, v, str, pvData
		; Create a random Boundary
		Local Boundary := this.RandomBoundary()
		Local BoundaryLine := "------------------------------" . Boundary

    this.Len := 0 ; GMEM_ZEROINIT|GMEM_FIXED = 0x40
    this.Ptr := DllCall( "GlobalAlloc", "UInt",0x40, "UInt",1, "Ptr"  )          ; allocate global memory

		; Loop input paramters
		For k, v in objParam
		{
			If IsObject(v) {
				For i, FileName in v
				{
					str := BoundaryLine . CRLF
					     . "Content-Disposition: form-data; name=""" . k . """; filename=""" . FileName . """" . CRLF
					     . "Content-Type: " . this.MimeType(FileName) . CRLF . CRLF
          this.StrPutUTF8( str )
          this.LoadFromFile( Filename )
          this.StrPutUTF8( CRLF )
				}
			} Else {
				str := BoundaryLine . CRLF
				     . "Content-Disposition: form-data; name=""" . k """" . CRLF . CRLF
				     . v . CRLF
        this.StrPutUTF8( str )
			}
		}

		this.StrPutUTF8( BoundaryLine . "--" . CRLF )

    ; Create a bytearray and copy data in to it.
    retData := ComObjArray( 0x11, this.Len ) ; Create SAFEARRAY = VT_ARRAY|VT_UI1
    pvData  := NumGet( ComObjValue( retData ) + 8 + A_PtrSize )
    DllCall( "RtlMoveMemory", "Ptr",pvData, "Ptr",this.Ptr, "Ptr",this.Len )

    this.Ptr := DllCall( "GlobalFree", "Ptr",this.Ptr, "Ptr" )                   ; free global memory 

    retHeader := "multipart/form-data; boundary=----------------------------" . Boundary
	}

  StrPutUTF8( str ) {
    Local ReqSz := StrPut( str, "utf-8" ) - 1
    this.Len += ReqSz                                  ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42
    this.Ptr := DllCall( "GlobalReAlloc", "Ptr",this.Ptr, "UInt",this.len + 1, "UInt", 0x42 )   
    StrPut( str, this.Ptr + this.len - ReqSz, ReqSz, "utf-8" )
  }
  
  LoadFromFile( Filename ) {
    Local objFile := FileOpen( FileName, "r" )
    this.Len += objFile.Length                     ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42 
    this.Ptr := DllCall( "GlobalReAlloc", "Ptr",this.Ptr, "UInt",this.len, "UInt", 0x42 )
    objFile.RawRead( this.Ptr + this.Len - objFile.length, objFile.length )
    objFile.Close()       
  }

	RandomBoundary() {
		str := "0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
		Sort, str, D| Random
		str := StrReplace(str, "|")
		Return SubStr(str, 1, 12)
	}

	MimeType(FileName) {
		n := FileOpen(FileName, "r").ReadUInt()
		Return (n        = 0x474E5089) ? "image/png"
		     : (n        = 0x38464947) ? "image/gif"
		     : (n&0xFFFF = 0x4D42    ) ? "image/bmp"
		     : (n&0xFFFF = 0xD8FF    ) ? "image/jpeg"
		     : (n&0xFFFF = 0x4949    ) ? "image/tiff"
		     : (n&0xFFFF = 0x4D4D    ) ? "image/tiff"
		     : "application/octet-stream"
	}

}
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: WinHttpRequest, Ragic, Uploading files

26 Aug 2019, 09:42

Works, fantastic. I have managed to upload a file.

Now I need to know how to upload a file in a field, and upload the text in other fields. Is there a way to combine both functions into one?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: WinHttpRequest, Ragic, Uploading files  Topic is solved

26 Aug 2019, 09:51

labrint wrote:
26 Aug 2019, 09:42
Now I need to know how to upload a file in a field, and upload the text in other fields. Is there a way to combine both functions into one?
It depends on the API supported or not. The uploading example above already sending files and text fields. You could try:

Code: Select all

objParam := { "api": ""
             , "v": "3"
             , "1000036": "test value"
             , "1000088": ["D:\your\file\path"] }
But you should look up the API documentation.
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: WinHttpRequest, Ragic, Uploading files

26 Aug 2019, 09:57

Thanks, I managed just about before I could reply to you. Thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 292 guests