• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CURL 等效于使用 VBA 的 POST JSON 数据

用户头像
it1352
帮助5

问题说明

我知道这与之前提出的一些问题类似,但有些问题仍然不适合我.怎么可以下面的命令:

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

有帮助的问题/答案:

但是,我仍然不知道如何在 VBA 中复制 CURL 语句

But, I'm still at a loss as to how to replicate the CURL statement in VBA

正确答案

#1

尝试进行基本授权,如下例所示:

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
系列文章
更多 icon
同类精品
更多 icon
继续加载