jeudi 30 mars 2017

Need help to post multipart form data using the post method in VBA

I am trying to use an API to update an online database.

The API is hosted by a french site and the usage details are slim with no examples.

This is the information they have provided

The query must be sent as a "multipart / form-data" html form using the "POST" method. 
Input parameters: 
ssid : (type "text") user's ScreenScraper identifier 
sspassword : (type "text") ScreenScraper password of the user 
gameid : (type "text") numerical identifier of the game on ScreenScraper 
or 
romid ( "text") numeric ID of the Roma on ScreenScraper 

to propose a textual info: 
modiftypeinfo ( "text") type of information sent (see list "modiftypeinfo") 
modifregion ( "text"





<html>
  <head>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  <META http-equiv="content-language" content="fr"> 
  <title></title>
  </head> 
  <body> 
    <form name="myform" id="myform" method="post" enctype="multipart/form-data" action="http://ift.tt/2oeZemR"> 
      <input type="text" name="gameid" value="3"> 
      <input type="text" name="modiftypeinfo" value="description">
      <input type="text" name="modifregion" value="">   
      <input type="text" name="modiflangue" value="fr"> 
      <input type="text" name="modifversion" value=""> 
      <input type="text" name="modiftexte" value="ici, le synopsis du jeu ! ne pas valider, c'est un test ;)"> 
      <input type="text" name="modifsource" value="botProposition / source de l'info">
      <input type="submit" value="envoyer"> 
    </form>

  <script> 
    document.getElementById("myform").submit();
    </script>

  </body>
</html> 

This is the code I have put together:

Sub Post_Detail()

    Dim xmlhttp As Object, response As String, SendString As String

    Set xmlhttp = CreateObject("MSXML2.XMLHTTP.6.0")

    '~~> Indicates that page that will receive the request and the type of request being submitted
    xmlhttp.Open "POST", "http://ift.tt/2oeZemR", False

    '~~> Indicate that the body of the request contains form data
    xmlhttp.setRequestHeader "Content-Type", "text/html; charset=UTF-8"
    xmlhttp.setRequestHeader "enctype", "multipart/form-data"

    '~~> Send the data as name/value pairs
    SendString = "&type=text&name=gameid&value=185882"
    SendString = SendString & "&type=text&name=modiftypeinfo&value=genres"
    SendString = SendString & "&type=text&name=modiftexte&value=Sports"
    SendString = SendString & "&type=submit&value=envoyer"
    Debug.Print SendString
    xmlhttp.send SendString

    response = xmlhttp.responseText
    MsgBox response

    Set xmlhttp = Nothing

End Sub

I am not sure what to do with this part: form name="myform" id="myform" in the instructions and I am not sure if my request via building a string is even anywhere near correct.

You won't be able to run without a login, it will return a password invalid error.




Aucun commentaire:

Enregistrer un commentaire