Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Func] HTTPRequest: for Web APIs [AHK-B + AHK-L+Unicode+x64]


  • Please log in to reply
380 replies to this topic
Erittaf
  • Members
  • 192 posts
  • Last active: Feb 28 2013 11:49 PM
  • Joined: 02 Nov 2007

I know what the AD credentials are but I haven't a clue how to format the header.  Anyone have experience with this?



kheechun
  • Members
  • 6 posts
  • Last active: Mar 03 2013 05:33 PM
  • Joined: 01 Mar 2009

Hi, I have a script that uses these options for HTTPRequest:

 

 
Options=
(
Charset: utf-8
SaveAs: Result.txt
Proxy: localhost:8888
)
 
When I check using a HTTP debugger, the response was:
{"success":"valid","submitted_code":"F2MA8QNC"}
 
However when I check the SaveAs file, it returns something like this:
?      玍*.MNN-.V睷侍+K躺LQ??錰枖う?绉?ボ?A J? 鵢?   
 
I think the response is in JSON format and compressed.
Is there anyone who can help me with this issue please? Thanks.


Wicked
  • Members
  • 504 posts
  • Last active: Nov 18 2018 02:17 AM
  • Joined: 07 Jun 2008
^. I wrote this for when I download simple text or a file:
_download(u,s=""){
wr:=comobjcreate("WinHttp.WinHttpRequest.5.1")
wr.open("GET",u)
wr.send()
wr.WaitForResponse(5)
if(wr.responsetext="failed"||wr.status!=200)
return false
if !s
return wr.responsetext
if(comobjtype(wr.ResponseStream)!=0xd)
return false
p:=comobjquery(wr.responsestream,"{0000000c-0000-0000-C000-000000000046}")
f:=fileopen(s,"w")
loop{
varsetcapacity(b,8192)
r:=dllcall(numget(numget(p+0)+3*a_ptrsize),"ptr",p,"ptr",&b,"uint",8192, "ptr*",c)
f.rawwrite(&b,c)
}until (c=0)
objrelease(p)
f.close()
return true
}
u is obviously the URL, s is the file to save to, if at all.

Ofc it could be more complex, but it does exactly what I needed it to. tongue.png.

3nL8f.png


VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006

What are the response headers? What version of internet explorer are you using?



kheechun
  • Members
  • 6 posts
  • Last active: Mar 03 2013 05:33 PM
  • Joined: 01 Mar 2009

What are the response headers? What version of internet explorer are you using?

I'm using IE10 on Windows 8.

 

Response Headers:

 

 HTTP/1.1 200 OK

Date Fri, 01 Mar 2013 22:59:14 GMT

Server Apache/2

X-Powered-By PHP/5.2.17

Expires Thu, 19 Nov 1981 08:52:00 GMT

Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma no-cache

Vary Accept-Encoding,User-Agent

Content-Encoding gzip

Content-Length 41

Keep-Alive timeout=1, max=100

Connection Keep-Alive

Content-Type text/html

 

I just found that the resultant save-as file can be decompressed using gzip and extracted.

Is there any way I can prevent httprequest from downloading it compressed?

Thanks happy.png



VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006

You can try using the "Accept-Encoding" header, e.g:

Accept-Encoding: gzip; q=0

Otherwise I'll have to tweak HTTPRequest according to http://msdn.microsof...5(v=vs.85).aspx in order for it to decompress gzip encoded content.



kheechun
  • Members
  • 6 posts
  • Last active: Mar 03 2013 05:33 PM
  • Joined: 01 Mar 2009

You can try using the "Accept-Encoding" header, e.g:

Accept-Encoding: gzip; q=0

Otherwise I'll have to tweak HTTPRequest according to http://msdn.microsof...5(v=vs.85).aspx in order for it to decompress gzip encoded content.

Thanks for the lead, I actually had gzip in my accept-encoding header. The solution was not to add gzip to the header but remove it. Once I removed the line, I managed to get a decompressed text. Thanks. Great help there happy.png



tmplinshi
  • Members
  • 245 posts
  • Last active: Mar 12 2015 02:29 PM
  • Joined: 06 Apr 2012
Could someone give me an example to upload image to wordpress site? I tried all night, and doesn't work.

My script is AHK_L Unicode. Thanks for your help.

Below is my code, it doesn't work:
url = %site_url%/wp-admin/media-upload.php?type=image&tab=type&post_id=%post_ID%
referer = %site_url%/wp-admin/media-upload.php?post_id=%post_ID%&


data =

img := d:\test.png

FileRead pack, %img%
FileGetSize size, %img%
SplitPath, img, file

Boundary := "7dd34b3614022e"
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.

data2 =
(
--%Boundary%
Content-Disposition: form-data; name="post_id"

%post_ID%
--%Boundary%
Content-Disposition: form-data; name="_wpnonce"

%_wpnonce%
--%Boundary%
Content-Disposition: form-data; name="_wp_http_referer"

/wp-admin/media-upload.php?type=image&tab=type&post_id=%post_ID%
--%Boundary%
Content-Disposition: form-data; name="html-upload"

涓婁紶

)

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

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

; Use the total size of the multipart data as the content length
headers2 := "Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*`n"
. "Content-Type: multipart/form-data; boundary=" Boundary "`n"
. "Content-Length: " StrLen( data )

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

HttpRequest(URL, out := data, out_h := Headers(referer) "`n" headers2)

FileDelete, Upload_Result.htm
FileAppend, % out, Upload_Result.htm
MsgBox, % out
ExitApp


VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006

See the example here for constructing multipart data for HTTPRequest.



tmplinshi
  • Members
  • 245 posts
  • Last active: Mar 12 2015 02:29 PM
  • Joined: 06 Apr 2012
Thank you VxE. I already tried that example, maybe something I did wrong. I will try it again.

Peregate
  • Members
  • 1 posts
  • Last active: May 03 2013 01:30 AM
  • Joined: 30 Apr 2013

Downloading files via redirected URL, where you don't know what the filename will be -- any idea how to get the filename, like a browser does?

 

I set up test URLs which emulate the two most common redirect situations I encounter:

url1 := "http://www.peregate.com/ahktest.pl?type=simple"
; 302 redirect to 'http://www.peregate.com/googlecalendarsync_installer.zip'

url2 := "http://www.peregate.com/ahkredirect"
; 301 redirect to 'http://www.peregate.com/ahktest.pl?type=double' then 302 redirect
;  to 'http://files.msassist.com/MSOffice/Outlook/googlecalendarsync_installer.exe'

bytes1 := httprequest(url1, resp1 := "", heads1 := "", "Binary`nSaveAs: unknown.zip")
bytes2 := httprequest(url2, resp2 := "", heads2 := "", "Binary`nSaveAs: unknown.exe")
MsgBox, [%url1%]`nHEADERS:`n%heads1%`n[%url2%]`nHEADERS:`n%heads2%

The last redirect "Location:" header usually contains the filename, but HTTPRedirect only stores the final (post-redirect) headers.

I tried dumping the buffer (to check for "Location:") at various points within HTTPRedirect, but wasn't able to catch it at the right moment.



VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006

Try the "+NO_AUTO_REDIRECT" option. That should allow you to handle the '3xx' responses individually.



Larctic
  • Members
  • 303 posts
  • Last active: May 10 2016 04:56 PM
  • Joined: 21 Jul 2012

I have a script, which is used to upload local images for google similar image search.
I tried to use the HttpRequest, in lieu of curl
But without success,
I would like to ask, you can help me with the HttpRequest achieve this script?

img = a_gif_from_deb.jpg

url=http://www.google.com.hk/searchbyimage/upload
_e=-e http://www.google.com.hk/imghp?hl=zh-CN
_A=-A "Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.15"

ToolTip, Uploading...
RunWait, curl -s %url% %_e% %_A% -F "[email protected]%img%" -o curl.tmp,, Hide
ToolTip

FileRead, out, curl.tmp
url_result := RegExReplace(out, "s)^.*The document has moved.*<A HREF=\x22([^\x22]+).*$", "$1")

if url_result
Run, %url_result%


VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
It looks like google's search by image requires a POST request with multipart data. Try using a browser plugin (like httpFox for firefox) to discover the details of the POST request.

There's an example of constructing a multipart POST payload here.

Larctic
  • Members
  • 303 posts
  • Last active: May 10 2016 04:56 PM
  • Joined: 21 Jul 2012

Thanks, I went to try.