I am trying to figure out how to make a POST in VBA. Ideally I'm looking for a simple working example that I can play with. This is what I have so far, but I'm not really sure what to do with it. Mostly what does the formdata look like.
Function WinHTTPPostRequest(URL, formdata, Boundary)
Dim http
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "POST", URL, False
'Set Content-Type header'
http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary
'Send the form data To URL As POST binary request'
http.send formdata
'Get a result of the script which has received upload'
WinHTTPPostRequest = http.responseText
End Function
Edit:
So I installed firebug so that I could get the object names for the "formdata" (see code). I would have thought formdata would look something like this "Form1=A&Form2=B". But it's still not working out. Any suggestions on how I should be doing this better?
Edit: So it seems there might be hidden fields that I need to send in my POST request.
To send form data in the format you suggest (i.e. identical to a GET request), I believe you need to set the Content-Type header to "application/x-www-form-urlencoded".
If you need to send more complex data (for example, including file uploads or other binary data), you might be better off setting the Content-Type to "multipart/form-data". The details of how to format the request body are laid out in RFC 2388, but you might be better off finding a library that will do it for you. It can be tricky getting the formatting exactly right, and there's no need to reinvent the wheel unless you're doing it as a learning experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With