本文介绍了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

有帮助的问题/答案:

但是,我仍然不知道如何在 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

这篇关于CURL 等效于使用 VBA 的 POST JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:05