Upload a file with POST (multipart/form-data) using VBA [duplicate] - vba

Does anyone know how to construct a POST DATA body in VBA? I'm trying to upload rather lengthy strings via a post call using the "Microsoft.XMLHTTP" object. I'm not tied to using that object for making the HTTP request either.

How about
Dim XMLHttp As Object: Set XMLHttp = CreateObject("Microsoft.XMLHTTP")
XMLHttp.Open "POST", "http://www.lfkfklkf.com", False
XMLHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XMLHttp.send "q=ploppy&x=cake"
Debug.print XMLHttp.responseText
Set XMLHttp = Nothing

Related

Create Sharepoint folder from VBA (access 2016)

I have an access 2016 app that currently creates a folder on a shared drive related to the DB record the user is working on.
We are migrating to SP online and I need to re-create something similar
I can list the files and folders currently in a directory using the API but when I try and create a new folder I am getting an error
"{"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}}"
The only thing I can see I am doing differently is I am not including a Authorization: "Bearer " + accessToken header in the post.
I haven't been including it in the gets for the Files list and it is working using the cached credentials from IE.
Getting the file and folder like this is working
'create XML HTTP object
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
With objXMLHTTP
'open connection to site
.Open "GET", url, False
'.send
.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
.setRequestHeader "Accept", "application/json"
.send
Do Until .ReadyState = 4: DoEvents: Loop
sJSONString = .responseText
End With
Dim Json As Object
Set Json = JsonConverter.ParseJson(sJSONString)
Debug.Print JsonConverter.ConvertToJson(Json, Whitespace:=2)
Dim value As Dictionary
For Each value In Json("value")
Debug.Print "> " + value("Name")
Next value
However trying to create a folder fails
strPostData = "{ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/Shared Documents/Folder1'}"
'Set objXMLHTTP = New MSXML2.XMLHTTP
With objXMLHTTP
.Open "POST", url, False
.setRequestHeader "accept", "application/json;odata=verbose"
.setRequestHeader "Content-Type", "application/json;odata=verbose"
.send strPostData
strResponse = .responseText
End With
Can anyone advise if what I am trying is possible and if so what I am doing is wrong?
Sorry, almost immediately I found and answer!
Needed to add
.setRequestHeader "X-RequestDigest", digest
.setRequestHeader "Content-Length", Len(strPostData)
and it all started working!
Thanks anyway, hope this helps someone else

VBA HTTP response text does not support Japanese

I have a VBA program that sends REST API request and get response.
I use WinHttp.WinHttpRequest to send request but the response text does not support Japanese.
It means if REST server returns result with Japanese, HTTP response text in VBA will be strange characters (e.g. ?aa??a).
I tried to SetRequestHeader with Accept-Language = ja-JP, Charset = UTF-8...but it does not work.
Please help me to resolve this problem.
My code here:
Dim oHttp As WinHttp.WinHttpRequest
Set oHttp = New WinHttp.WinHttpRequest
oHttp.Open "GET", "url", False
oHttp.SetRequestHeader "Accept", "application/json"
oHttp.SetRequestHeader "Content-Type", "application/json"
oHttp.SetRequestHeader "Authorization", "Bearer <token>")
oHttp.Send (sData)
Debug.Print oHttp.ResponseText
Thanks
I found a solution for that: use ServerXMLHTTP instead of WinHttpRequest.

Web Scraping using VBA and MSXML2.XMLHTTP library

I'm trying to scrap data from a website using MSXML2.XMLHTTP object on VBA environment (Excel) and I cannot figure out how to solve this problem! The website is the following:
http://www.detran.ms.gov.br/consulta-de-debitos/
You guys can use the following test data to fill the form:
Placa: oon5868
Renavam: 1021783231
I want to retrieve data like "chassi", with the data above that would be " 9BD374121F5068077".
I do not have problems parsing the html document, the difficult is actually getting the information as response! Code below:
Sub SearchVehicle()
Dim strPlaca As String
Dim strRenavam As String
strPlaca = "oon5868"
strRenavam = "01021783231"
Dim oXmlPage As MSXML2.XMLHTTP60
Dim strUrl As String
Dim strPostData As String
Set oXmlPage = New MSXML2.XMLHTTP60
strUrl = "http://www2.detran.ms.gov.br/detranet/nsite/veiculo/veiculos/retornooooveiculos.asp"
strPostData = "placa=" & strPlaca & "&renavam=" & strRenavam
oXmlPage.Open "POST", strUrl, False
oXmlPage.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXmlPage.send strPostData
Debug.Print oXmlPage.responseText
End Sub
The strURL used in the POST method ".../retornooooveiculos.asp" is the one google developer tools and fiddler showed me that was the correct address the website was posting the payload.
When manually accessed, the website retrieve the correct information, but running my code I always get the following response on the .responseText:
<html>Acesse: <b><a href='http://www.detran.ms.gov.br target='_parent'>www.detran.ms.gov.br</a></b></html>
HELP PLEASE, I'm getting crazy trying to solve this puzzle! Why do I get redirected like this?
I need the "CHASSI" information and can't find the correct http Request to do this!
Try the below approach. It should fetch you the content you are after. The thing is you need to supply the Cookie copied from your Request Headers fields in order for your script to work which you can find using devtools.
Sub SearchVehicle()
Const URL As String = "http://www2.detran.ms.gov.br/detranet/nsite/veiculo/veiculos/retornooooveiculos.asp"
Dim HTTP As New ServerXMLHTTP60, HTML As New HTMLDocument
Dim elem As Object, splaca$, srenavam$, qsp$
splaca = "oon5868"
srenavam = "01021783231"
qsp = "placa=" & splaca & "&renavam=" & srenavam
With HTTP
.Open "POST", URL, False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "Cookie", "ISAWPLB{07D08995-E67C-4F44-91A1-F6A16337ECD6}={286E0BB1-C5F9-4439-A2CE-A7BE8C3955E0}; ASPSESSIONIDSCSDSCTB=AGDPOBEAAPJLLMKKIGPLBGMJ; 69137927=967930978"
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send qsp
HTML.body.innerHTML = .responseText
End With
For Each elem In HTML.getElementsByTagName("b")
If InStr(elem.innerText, "Chassi:") > 0 Then MsgBox elem.ParentNode.NextSibling.innerText: Exit For
Next elem
End Sub
Once again: fill in the Cookie field by collecting it using your devtools (from Request Headers section), if for some reason my provided Cookie doesn't work for you. Thanks.
Output I'm getting:
9BD374121F5068077

POST not working, while GET works fine (xmlhttp)

I'm writing an Excel macro to communicate with my API, and it seems I cannot send an request via POST, only GET is working.
My code:
Private Sub button1_Click()
Dim xmlhttp As New MSXML2.xmlhttp, myurl As String
myurl = "https://{my_url}"
xmlhttp.Open "POST", myurl, False
xmlhttp.setRequestHeader "Content-Type", "application/xml"
xmlhttp.send
MsgBox (xmlhttp.responseText)
End Sub
Macro run returns:
No resource method found for POST, return 405 with Allow header
When I modify the code, changing POST to GET:
xmlhttp.Open "GET", myurl, False
It works fine, I get the response. How can I force excel to cooperate with POST method? Since my API for more advanced communication requires POST?
Upon further investigation there was nothing wrong with my VBA code. The response I got, was sent from my API. Thank you for comments, they helped me to resolve the issue by including XML body into xmlhttp.send command.

Creating a POST body in VBA

Does anyone know how to construct a POST DATA body in VBA? I'm trying to upload rather lengthy strings via a post call using the "Microsoft.XMLHTTP" object. I'm not tied to using that object for making the HTTP request either.
How about
Dim XMLHttp As Object: Set XMLHttp = CreateObject("Microsoft.XMLHTTP")
XMLHttp.Open "POST", "http://www.lfkfklkf.com", False
XMLHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XMLHttp.send "q=ploppy&x=cake"
Debug.print XMLHttp.responseText
Set XMLHttp = Nothing