CURL等同于使用VBA的POST JSON数据 [英] CURL Equivalent to POST JSON data using VBA

查看:257
本文介绍了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的托管TIN CAN(xAPI)学习记录存储(由SaltBox提供)有关.上面的例子来自这里: 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

我有一个帐户(免费修补匠帐户,无需设置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

有帮助的问题/答案:

  • Send a JSON string to a RESTful WS from Classic ASP
  • https://stackoverflow.com/a/17063741/3451115
  • How to POST JSON Data via HTTP API using VBScript?

但是,关于如何在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数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆