Upload image to Google Images failed

Get help with using AutoHotkey and its commands and hotkeys
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Upload image to Google Images failed

23 Feb 2014, 15:07

The curl code is very simple:
curl http://www.google.com/searchbyimage/upload -F "[email protected]"

Code I've tried:
Spoiler
Edit: I got it working:

Code: Select all

; Upload image to Google Images
; (Tested on AHK A32)

#NoEnv
#Include <HTTPRequest> ; http://www.autohotkey.com/board/topic/67989-func-httprequest-for-web-apis-ahk-b-ahk-lunicodex64/

ImagePath := "smile.gif"

FileRead, pack, *c %ImagePath%
FileGetSize, size, %ImagePath%
SplitPath, ImagePath, ImageFileName

Boundary := "b03f7ef85666"
CRLF := "`r`n"
DW := "UInt"
ptr := (A_PtrSize = "") ? DW : "Ptr"

; For AHK ANSI ONLY. To insert binary data into a string (such as multipart data).
VarSetCapacity( data, size + 500, 1 ) ; create a string of nonzero bytes longer than the binary data.

; Fill the multipart data with plain text, using the nonzero bytes as a placeholder for the binary data.
data := "------------------------------" Boundary CRLF
. "Content-Disposition: form-data; name=""encoded_image""; filename=""" . ImageFileName . """" CRLF
. "Content-Type: image/gif" CRLF CRLF
. SubStr( data, 1, size ) CRLF
. "------------------------------" Boundary "--" CRLF

; Calculate the offset at which the placeholder begins
offset := StrLen( data ) - size - StrLen( CRLF "------------------------------" Boundary "--" CRLF )

; Use the total size of the multipart data as the content length
headers := "Accept: */*`n"
. "Content-Length: " StrLen( data ) "`n"
. "Expect: 100-continue`n"
. "User-Agent: Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.15`n"
. "Content-Type: multipart/form-data; boundary=----------------------------" Boundary

; Copy the binary data into the string, overwriting the placeholder bytes
DllCall( "RtlMoveMemory", Ptr, &data + offset, Ptr, &pack, DW, size )

url := "http://www.google.com/searchbyimage/upload"
HttpRequest(URL, InOutData := data, InOutHeader := headers, "+NO_AUTO_REDIRECT`n+Flag: INTERNET_FLAG_NO_COOKIES")
Run, % RegExReplace(InOutHeader, "^.*Location: ([^\r\n]+).*$", "$1")
Return
But I still have two questions:
  1. How to work with AHK Unicode?
  2. How to work using WinHttp.WinHttpRequest.5.1? I've tried it, the binary data disappeared.
Thanks![/color]
Attachments
smile.gif
smile.gif (659 Bytes) Viewed 5019 times
Last edited by tmplinshi on 26 Feb 2014, 05:24, edited 6 times in total.
User avatar
VxE
Posts: 44
Joined: 30 Sep 2013, 10:35
Location: Simi Valley, CA

Re: Upload image to Google Images failed

23 Feb 2014, 17:56

It looks like google image search expects the image to be base-64 encoded (inside the multipart form). You can probably prepare the POST body as plain text.
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Upload image to Google Images failed

24 Feb 2014, 00:48

Thanks. Could you give me an example?

I've captured the packets using SmartSniff, they looks the same, see below images:
Spoiler
I even had compared their packets in hex format, the Boundary part is same.
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Upload image to Google Images failed

24 Feb 2014, 08:40

I also tried another website http://uploadpie.com/, failed again. :(
Code I've tried:
Spoiler
The cURL working code is:
curl http://uploadpie.com/ -F "[email protected]" -F "expire=1" -F "MAX_FILE_SIZE=3145728" -F "upload=1"
Last edited by tmplinshi on 27 Feb 2014, 08:35, edited 1 time in total.
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Upload image to Google Images failed

26 Feb 2014, 02:22

Got it working! :lol: The dashes in Content-Type: should be two less than the dashes in the data.

For example in below packets, the red line has 28 characters, and the blue line has 30 characters.
POST /searchbyimage/upload HTTP/1.1
User-Agent: curl/7.31.0
Host: http://www.google.com
Accept: */*
Content-Length: 854
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------ed0ce168cf80

------------------------------ed0ce168cf80
Content-Disposition: form-data; name="encoded_image"; filename="smile.gif"
Content-Type: image/gif
So the working code are:
Spoiler
Last edited by tmplinshi on 26 Feb 2014, 05:09, edited 1 time in total.
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Upload image to Google Images failed

26 Feb 2014, 04:41

But I still have two questions:
  • How to work with AHK Unicode?
  • How to work using WinHttp.WinHttpRequest.5.1? I've tried it, the binary data disappeared:

    Code: Select all

    WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    WebRequest.Open("POST", url)
    WebRequest.setRequestHeader("Content-Type"  , "multipart/form-data; boundary=----------------------------" Boundary)
    WebRequest.setRequestHeader("Content-Length", StrLen(data))
    WebRequest.Option(6) := False ; Disable auto redirect
    WebRequest.Send(data)
    Run, % WebRequest.GetResponseHeader("Location")
    Return
    packets:
    Spoiler
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Upload image to Google Images failed

27 Feb 2014, 11:42

Can somebody help me? :( I don't know how to make changes, in order to run in Unicode AHK.
I also tried this example code by VxE, but the final posting data is empty:

Code: Select all

; This code doesn't work on both ANSI and Unicode. For the ANSI working code, please see the #1 post.
#NoEnv

package := "smile.gif"
FileRead pack, %package%
FileGetSize size, %package%
SplitPath package, file

Boundary := "b03f7ef85666"
CRLF := "`r`n"
DW := "UInt"
ptr := (A_PtrSize == "") ? DW : "Ptr"

; For AHK UNICODE or ANSI. To assemble a multipart payload with UTF-8 text and binary data
VarSetCapacity( data, size + 4096 ) ; Allocate enough space in a var for both the data and text

leadingtext := "------------------------------" Boundary CRLF
	. "Content-Disposition: form-data; name=""encoded_image""; filename=""" . file . """" CRLF
	. "Content-Type: image/gif" CRLF CRLF

; Use StrPut to both convert and insert the leading text component of the multipart data.
; Also, StrPut returns the offset at which the binary data should be inserted.
offset := StrPut( leadingtext, &data + 0, StrLen( leadingtext ), "UTF-8" )

; Copy the binary data into the variable at the offset
DllCall("RtlMoveMemory", Ptr, &data + offset, Ptr, &pack, DW, size )

; Once more, use StrPut to convert and insert text. This time it's the final boundary
size += offset + StrPut( CRLF "------------------------------" Boundary "--" CRLF
	                   , &data + offset + size
	                   , StrLen( CRLF "------------------------------" Boundary "--" CRLF)
	                   , "UTF-8" )

; Use the sum of the lengths of the parts as the total size of the POST data
headers := "User-Agent: Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.15`n"
         . "Content-Type: multipart/form-data; boundary=----------------------------" Boundary

MsgBox % (A_IsUnicode ? "Unicode" : "ANSI") "`n`n" headers "`n`n" StrGet(&data, "UTF-8")

; Submit the request
url     := "http://www.google.com/searchbyimage/upload"
Options :=   "+Flag: INTERNET_FLAG_NO_COOKIES`n+NO_AUTO_REDIRECT"
           . (A_IsUnicode ? "`nCharset: UTF-8" : "")
HttpRequest(URL, data, headers, Options)
MsgBox % (A_IsUnicode ? "Unicode" : "ANSI") "`n`n" headers
:arrow: Testing files: https://www.dropbox.com/s/xbetn61bbf87z ... st_Test.7z
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: Upload image to Google Images failed

29 Mar 2015, 13:06

Check out the code I posted in this post: http://ahkscript.org/boards/viewtopic.p ... 038#p21986
My code runs fine on unicode.
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Upload image to Google Images failed

29 Mar 2015, 22:50

Bruttosozialprodukt wrote:Check out the code I posted in this post: http://ahkscript.org/boards/viewtopic.p ... 038#p21986
My code runs fine on unicode.
I've tried that post before, but at that time there's some bugs. Seems you've fixed it now, I'll try it. Thanks! :)

Return to “Ask For Help”

Who is online

Users browsing this forum: c7aesa7r, Google [Bot], mcl, mikeyww, svArtist, TheProdigyC2, WOlfen, zKade and 51 guests