0

I've been trying to post data to a server using multipart/form-data however the server doesn't seem to receieve anything.

VB Code

' create a boundary consisting of a random string
strBoundary = RandomAlphaNumString(32)

strBody = "--" & strBoundary & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name=""test1""" & vbCrLf & vbCrLf & STRING
strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name=""data""" & vbCrLf & vbCrLf & data
strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name=""data2""" & vbCrLf & vbCrLf & data2
strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name=""data3""" & vbCrLf & vbCrLf & data3
strBody = strBody & vbCrLf & "--" & strBoundary & "--" 

' Content-Length
sHttpLength = Len(strBody)

Set WinHttpReq = New WinHttpRequest
strURL = "https://" & HOST & URL ' directed to test.php

 hostHeader = HOST & vbCrLf
contentTypeHeader = "multipart/form-data; boundary=" & strBoundary & vbCrLf
contentLengthHeader = sHttpLength & vbCrLf & vbCrLf

     WinHttpReq.Open "POST", strURL, False 'Open a Http connection
     WinHttpReq.SetRequestHeader "HOST", hostHeader
     WinHttpReq.SetRequestHeader "Content-Type", contentTypeHeader
     WinHttpReq.SetRequestHeader "Content-Length", contentLengthHeader

WinHttpReq.Send strBody ' Send Post messages

The server is receiving the request as it sends back data to the vb app however it doesn't recognise the posted pairs

e.g.

$postedVal = isset($_POST["test1"]) ? $_POST["test1"] : '';

This returns '' showing that the data hasn't been received correctly.

Is there any major flaw I'm not seeing?

Any advice would be great.

dan983
  • 454
  • 2
  • 5
  • 19

1 Answers1

0

It turns out that if you don't specify the Charset in the content-Type header it automatically assigns UTF-8 at the end of the header. This results in the posted message not working! Solution manually enter the Charset before the boundary. Now it works fine.....tough error to find when you keep checking the boundary!

E.g.

contentTypeHeader = "multipart/form-data;Charset=UTF-8; boundary=" & strBoundary & vbCrLf
dan983
  • 454
  • 2
  • 5
  • 19