VBA HTTP response text does not support Japanese - vba

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.

Related

How to send a GET request with a body using VBA?

I want to retrieve some data from an API using an GET request. The API takes one parameter encoded as JSON in the body of the request. The following code works fine for sending a POST request with some JSON data however if the type of the request is changed to GET the final request being sent contains no body:
Dim strJSONText As String, strURL As String
strURL = "https://<domain>/<some>/<path>"
strJSONText = "{'<parameter>':'<value>'}"
Set objXMLhttp = CreateObject("Msxml2.XMLHTTP")
objXMLhttp.Open "GET", strURL, False
objXMLhttp.setRequestHeader "Accept", "application/json"
objXMLhttp.setRequestHeader "Content-Type", "application/json"
objXMLhttp.send (strJSONText)
Why is strJSONText being ignored whenever the request type is GET? How do I send a GET request with a body using VBA?

Excel VBA MSXML2 Post response body empty runtime error -2147467260

I would like to post data to Jira using this method :
Private IEService As New MSXML2.XMLHTTP60
Public Function httpPOST(baseUrl As String, url As String, jsonInput As String, auth As String) As String
Dim accept As String
accept = "application/json"
With IEService
.Open "POST", baseUrl & url, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", accept
.setRequestHeader "Authorization", "Basic " & auth
.setRequestHeader "Origin", baseUrl
.send jsonInput
httpPOST = .responseText
'MsgBox (.responseText)
End With
End Function
Data is fine and the POST is executed on the back end side. But Jira in this instance returns an empty response body.
When this happens VBA crashes on .send jsonInput with error : runtime error -2147467260
Seems like MSXML2 does not handle empty responses. But I haven't found anyone facing same issue searching on the web.
Any ideas what I am doing wrong or how to fix it ?
Thanks

POST request created in vba brings back nothing as result

I've written a very tiny script in vba using POST request. However, when I run it, I get nothing as result except for a blank message. I've tried to fill in the request parameter accordingly. Perhaps, I can't notice which should be included in the parameter. The page I'm dealing with contains several images in it's right panel. When an image is clicked the request about which i'm talking here is sent to the server and brings back the result and displays new information concerning its' flavor under it. My goal is to parse all the flavors connected to each images. Anyways, I'm trying to attach all the things necessary to find out what i'm missing. Thanks in advance.
This is what I got from chrome developer tools to prepare the POST request:
"https://www.dropbox.com/s/zjn0ahixhu58miq/RequestStatus.txt?dl=0"
Here is what I'm trying with:
Sub PostReq()
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim ArgumentStr As String
ArgumentStr = "opt=flavor&opt1=207&opt2=47&ip=105"
With http
.Open "POST", "https://www.optigura.com/product/ajax/details.php", False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
.send ArgumentStr
html.body.innerHTML = .responseText
End With
MsgBox http.responseText
End Sub
This is the original link to the webpage:
"https://www.optigura.com/uk/product/gold-standard-100-whey/"
Your code sets a request header like so:
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
so the script is going to expect the argument string to be URL encoded (which it isn't).
Try either encoding the string, or send the request using "GET" instead.
Finally, I've made it. To receive the required response it is necessary to send a GET request first then again send a POST request using the response from that get request. Here is the working one:
Sub httpPost()
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim ArgumentStr As String
ArgumentStr = "opt=flavor&opt1=207&opt2=47&ip=105"
With http
.Open "GET", "https://www.optigura.com/uk/product/gold-standard-100-whey/", False
.send
End With
With http
.Open "POST", "https://www.optigura.com/product/ajax/details.php", False
.setRequestHeader "X-Requested-With", "XMLHttpRequest"
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
.send ArgumentStr
html.body.innerHTML = .responseText
End With
MsgBox http.responseText
End Sub

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

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

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