Custom header with MSXML2.ServerXMLHTTP

北城余情 提交于 2019-12-23 04:58:15

问题


I am currently trying to use MSXML2.ServerXMLHTTP to send a POST http request. I need to add a custom header "Auth" so that my requests get authorized but it doesn't seem to work. Here is the code for my post function:

Function post(path As String, authToken As String, postData As String) As String
  Dim xhr As Object
  Dim message As String
On Error GoTo error:

  Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
  xhr.Open "POST", path, False
  xhr.setRequestHeader "Content-Type", "application/json"
  xhr.setRequestHeader "Auth", authToken
  xhr.send postData

  If xhr.Status = 200 Then
    message = xhr.responseText
  Else
    message = xhr.Status & ": " & xhr.statusText
  End If

  Set xhr = Nothing
  post = message
error:
  Debug.Print "Error " & Err.Number; ":" & Err.Description
End Function

And I end up with "Error -2147012746 Requested Header was not found" (The message is actually translated since I use a different language on my computer).

However I didn't have this problem with the "Microsoft.XMLHTTP" Object. Am I doing something wrong ?

Thank you for your time.


回答1:


Try changing the call to SetRequestHeader to use a string literal. I duplicated the problem when authToken does not have a value set.

Change from this

xhr.setRequestHeader "Auth", authToken

To this

xhr.setRequestHeader "Auth", "testdatahere"


来源:https://stackoverflow.com/questions/37028710/custom-header-with-msxml2-serverxmlhttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!