CURL 等效于使用 VBA 的 POST JSON 数据
问题说明
我知道这与之前提出的一些问题类似,但有些问题仍然不适合我.怎么可以下面的命令:
I know this is similar to some previously asked questions, but something is still not working for me. How can the following command:
curl -X POST --data @statements.json -H "Content-Type: application/json" --user username:password -H "x-experience-api-version: 1.0.0" https://MYLRS.waxlrs.com/TCAPI/statements
在 VBA 中复制?
额外信息:
这与名为 WaxLRS(SaltBox 提供)的托管 TIN CAN (xAPI) 学习记录存储有关.上面的例子来自这里:http://support.saltbox.com/support/solutions/articles/1000083945-快
This relates to a Hosted TIN CAN (xAPI) Learning Record Store called WaxLRS (by SaltBox). The above example comes from here: http://support.saltbox.com/support/solutions/articles/1000083945-quick
我有一个帐户(免费的 tinkerers 帐户,无需设置 CC)并生成了我认为是必需的用户名和密码组合.凭证被称为标识符"和密码"并出现在标题下:基本身份验证凭据.
I have an account (free tinkerers account, no CC required to setup) and have generated what I believe to be the required username & password combination. The credentials are termed 'Identifier' & 'Password' and appear under a heading: Basic Authentication Credentials.
无论我做什么,我都会收到一条错误消息:
No matter what I do I get an error message:
<html>
<head><title>Unauthorized</title></head>
<body>
<h1>Unauthorized</h1>
<p>This server could not verify that you are authorized to
access the document you requested. Either you supplied the
wrong credentials (e.g., bad password), or your browser
does not understand how to supply the credentials required.
<br/>
<!-- --></p>
<hr noshade>
<div align="right">WSGI Server</div>
</body>
</html>
我相信该示例期望从文件中获取 JSON 有效负载,但我将其加载到字符串中.我不希望这会导致问题,我已经将我的字符串与使用 NP Compare 提供的示例进行了比较并且它匹配.
I believe that the example is expecting the JSON payload to be obtained from a file, but I am loading it into a string. I don't expect this to be contributing to the problem, I have compared my string with the example provided using NP Compare and it matches.
到目前为止我的代码是:
My code so far is:
url = "https://xxxxxxx.waxlrs.com/TCAPI/statements"
Set pXmlHttp = CreateObject("WinHttp.WinHttpRequest.5.1") 'MSXML2.XMLHTTP")
pXmlHttp.Open "POST", url, False
pXmlHttp.setRequestHeader "Content-Type", "application/json"
'pXmlHttp.setRequestHeader "Authorization", "Basic xxxxxxt8wfB6JYerYCz:xxxxxx1FOd29J1s6G2"
pXmlHttp.SetCredentials "xxxxxxt8wfB6JYerYCz", "xxxxxx1FOd29J1s6G2", 0
pXmlHttp.setRequestHeader "x-experience-api-version", "1.0.0"
pXmlHttp.send (stringJSON)
Set pHtmlObj = CreateObject("htmlfile")
pHtmlObj.body.innerHTML = pXmlHttp.responseText
apiWaxLRS = pXmlHttp.responseText
有帮助的问题/答案:
- 从经典 ASP 向 RESTful WS 发送 JSON 字符串
- https://stackoverflow.com/a/17063741/3451115
- 如何使用 VBScript 通过 HTTP API POST JSON 数据?
但是,我仍然不知道如何在 VBA 中复制 CURL 语句
But, I'm still at a loss as to how to replicate the CURL statement in VBA
正确答案
尝试进行基本授权,如下例所示:
Try to make basic authorization as shown in the below example:
Sub Test()
sUrl = "https://xxxxxxx.waxlrs.com/TCAPI/statements"
sUsername = "*******************"
sPassword = "******************"
sAuth = TextBase64Encode(sUsername & ":" & sPassword, "us-ascii")
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "POST", sUrl, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Basic " & sAuth
.setRequestHeader "x-experience-api-version", "1.0.0"
.send (stringJSON)
apiWaxLRS = .responseText
End With
End Sub
Function TextBase64Encode(sText, sCharset)
Dim aBinary
With CreateObject("ADODB.Stream")
.Type = 2 ' adTypeText
.Open
.Charset = sCharset
.WriteText sText
.Position = 0
.Type = 1 ' adTypeBinary
aBinary = .Read
.Close
End With
With CreateObject("Microsoft.XMLDOM").CreateElement("objNode")
.DataType = "bin.base64"
.NodeTypedValue = aBinary
TextBase64Encode = Replace(Replace(.Text, vbCr, ""), vbLf, "")
End With
End Function
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhbjfiag
-
检测是否安装了 Microsoft Edge?
it1352 08-02 -
从iPhone上的来电获取来电者的电话号码
it1352 01-22 -
事件处理程序引发方法约定
it1352 07-21 -
获取我的整个 YouTube 观看历史记录?
it1352 08-03 -
允许在键盘 iOS10 进行完全访问检查
it1352 09-19 -
字母数字电话号码转换为数字
it1352 07-20 -
修复Visual Studio 2022内存不足问题
it1352 06-08 -
串口通信对校验和的需求
it1352 06-06 -
当有人访问它时,在我的主页上自动弹出图像?
it1352 07-25 -
为什么在 Array 的子类设置位置不会改变它的长度?我不应该继承数组吗?
it1352 07-15