Jump to content

Cannot send POST request with WinHttp functions


W2lkm2n
 Share

Recommended Posts

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 :D)

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:

#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.txt

I 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 by W2lkm2n
Link to comment
Share on other sites

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

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

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

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)

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 by W2lkm2n
Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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:

#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

  • 1 month later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...