W2lkm2n Posted September 9, 2012 Share Posted September 9, 2012 (edited) After three days of testing, reading posts, triing different methods, I cannot make a simple POST request work with (v. 1.6.2.5)I can't even debug because Fiddler2 doesn't capture data sent by autoit for some reason... and capturing with Wireshark seems ok (not sure about this one tough )I even checked page__st__20#entry610967, but I think I generating the POST body the same way, still not working.When I run it, nothing happens on server side, I can confirm the headers I sent, but I can't even debug the request body, because it seems like PHP is not able to print out RAW POST request body, when "multipart/form-data" is used... (neither php://input, nor HTTP extension does). I was able to post "application/x-www-form-urlencoded" forms with no problem at all.So here is the code in the simplest form which is not working:expandcollapse popup#include "WinHttp.au3" Opt("MustDeclareVars", 1) ; Initialize and get session handle Global $hOpen = _WinHttpOpen() ; Get connection handle Global $hConnect = _WinHttpConnect($hOpen, "dev.walkman.pk") ; Specify the reguest Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", "HHupload/index.php") ;~ -------------------------------------------------------- Global $sFileName = "Walk_teszt.txt" Global $sFullFile = @DesktopDir & "" & $sFileName Global $sBoundary = "------WALKMANlaksdjf" Global $sS $sS = $sBoundary & @CRLF $sS &= 'Content-Dispositon: form-data; name="file"; filename="' & $sFileName & '"' & @CRLF $sS &= "Content-Type: text/plain" & @CRLF & @CRLF & FileRead($sFullFile) & @CRLF $sS &= $sBoundary & "--" & @CRLF ; Send request Global $hSendRequest = _WinHttpSendRequest($hRequest, "Content-Type: multipart/form-data; boundary=" & $sBoundary & @CRLF, $sS, StringLen($sS)) ; Wait for the response _WinHttpReceiveResponse($hRequest) ; Check if there is data available... If _WinHttpQueryDataAvailable($hRequest) Then MsgBox(64, "OK ?", _WinHttpReadData($hRequest)) Else MsgBox(48, "Error", "Site is experiencing problems (or you).") EndIf ; Close handles _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen)Here is the sample txt file I send.Walk_teszt.txtI even tried_WinHttpAddRequestHeaders($hRequest, "Content-Type: multipart/form-data; boundary=" & $sBoundary & @CRLF) Global $hSendRequest = _WinHttpSendRequest($hRequest, Default, Default, 234) Global $hWrite = _WinHttpWriteData($hRequest, $sS)with no success.Can anybody tell me what's wrong with this ? Edited September 9, 2012 by W2lkm2n Link to comment Share on other sites More sharing options...
dany Posted September 9, 2012 Share Posted September 9, 2012 As far as I can tell the error is on the php side: If you send files with multipart/form-data the files will be in $_FILES global array and not $HTTP_RAW_POST_DATA. [center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF Link to comment Share on other sites More sharing options...
W2lkm2n Posted September 9, 2012 Author Share Posted September 9, 2012 As far as I can tell the error is on the php side: If you send files with multipart/form-data the files will be in $_FILES global array and not $HTTP_RAW_POST_DATA.There is nothing in the $_FILES array. I tried write out $HTTP_RAW_POST_DATA to see if maybe there is an error, so the request is not recognized as file upload... Link to comment Share on other sites More sharing options...
dany Posted September 9, 2012 Share Posted September 9, 2012 Mmmh, I notice you're using your boundary different as from the standards. Change these lines and try again: ; This declaration... Global $sBoundary = "WALKMANlaksdjf" ; And this concat operation... $sS &= "--" & $sBoundary & "--" & @CRLF Also, shouldn't this: $sS &= 'Content-Dispositon: form-data; name="file"; filename="' & $sFileName & '"' & @CRLF actually be: $sS &= 'Content-Type: multipart/form-data; name="file"; filename="' & $sFileName & '"' & @CRLF [center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF Link to comment Share on other sites More sharing options...
ProgAndy Posted September 9, 2012 Share Posted September 9, 2012 The boundary is not different from the standards, but the boundary set in the header must have 2 hyphens less at the beginning than used in the body. Fields are separated with newline, two hyphens, boundary, newline Here is a working example (if the api of the website wasn't changed) *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
W2lkm2n Posted September 9, 2012 Author Share Posted September 9, 2012 (edited) The boundary is not different from the standards, but the boundary set in the header must have 2 hyphens less at the beginning than used in the body. Fields are separated with newline, two hyphens, boundary, newlineHere is a working example (if the api of the website wasn't changed)The API wasn't changed but I always get the same errorcode (api key not found). It might because Autoit doesn't send POST data right.Would somebody confirm that script is working without error ? I suspect that something is wrong with my Windows or I found a bug or something... Edited September 9, 2012 by W2lkm2n Link to comment Share on other sites More sharing options...
trancexx Posted September 9, 2012 Share Posted September 9, 2012 It's easy to test what's wrong. Make your HHupload/index.php look like this: <!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Request Headers</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > </head> <body> <pre> <?php print_r(apache_request_headers()); print_r($_FILES); ?> </pre> </body> </html> ...and then use this script to upload file: #include "WinHttp.au3" Global $hOpen = _WinHttpOpen() Global $sAddress = "http://dev.walkman.pk/HHupload/index.php" ; This form doesn't exist on server side! Local $sLocalForm = _ '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & @CRLF & _ ' <input type="file" id="file" name="file" />' & @CRLF & _ '</form>' Local $sRead = _WinHttpSimpleFormFill($sLocalForm, $hOpen, Default, "file", @ScriptFullPath) _WinHttpCloseHandle($hOpen) ConsoleWrite($sRead & @CRLF) MsgBox(64, "Read", $sRead) ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
W2lkm2n Posted September 9, 2012 Author Share Posted September 9, 2012 I had exactly the same two functions on the server, and I got only the headers back, no BODY at all, when I tried to send my request in OP.However your solution works like a charm, thank you very much !!! I will use this and now I can sleep However I would like to understand what was the problem with my code if you would be kind enough to help me.So I made the changes what ProgAndy suggested, and still got nothing back from body... Now it looks like this and still no good:expandcollapse popup#include "WinHttp.au3" Opt("MustDeclareVars", 1) ; Initialize and get session handle Global $hOpen = _WinHttpOpen() ; Get connection handle Global $hConnect = _WinHttpConnect($hOpen, "dev.walkman.pk") ; Specify the reguest Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", "HHupload/index.php") ;~ -------------------------------------------------------- Global $sFileName = "Walk_teszt.txt" Global $sFullFile = @DesktopDir & "" & $sFileName Global $sBoundary = "------WALKMANlaksdjf" Global $sS $sS = @CRLF & "--" & $sBoundary & @CRLF $sS &= 'Content-Dispositon: form-data; name="file"; filename="' & $sFileName & '"' & @CRLF $sS &= "Content-Type: text/plain" & @CRLF & @CRLF & FileRead($sFullFile) & @CRLF $sS &= "--" & $sBoundary & "--" & @CRLF ; Send request Global $hSendRequest = _WinHttpSendRequest($hRequest, "Content-Type: multipart/form-data; boundary=" & $sBoundary & @CRLF, $sS, StringLen($sS)) ; Wait for the response _WinHttpReceiveResponse($hRequest) ; Check if there is data available... If _WinHttpQueryDataAvailable($hRequest) Then MsgBox(64, "OK ?", _WinHttpReadData($hRequest)) Else MsgBox(48, "Error", "Site is experiencing problems (or you).") EndIf ; Close handles _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen)Also, if the REQUEST body is not well formatted, I have no idea how to debug... because PHP won't have $_FILES set and 'php://input' wrapper doesn't work on multipart/form-data forms.Any idea ?Thanks anyway if you don't care anymore Link to comment Share on other sites More sharing options...
trancexx Posted September 9, 2012 Share Posted September 9, 2012 You wrote Content-Dispositon Read that letter by letter, you'll see what you did wrong. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
W2lkm2n Posted September 9, 2012 Author Share Posted September 9, 2012 You wrote Content-Dispositon Read that letter by letter, you'll see what you did wrong. Man, I can't believe I pulled my hair out because of this typo ! THANK YOU SO MUCH Link to comment Share on other sites More sharing options...
R2D2 Posted October 12, 2012 Share Posted October 12, 2012 I can't even debug because Fiddler2 doesn't capture data sent by autoit for some reason.fiddler2 captures data from applications that use fiddle proxy . Set proxy "127.0.0.1:8888" in your autoit code if you want to use fiddler Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now