I am making a server-side HTTP request with a JSON body in VBScript like this:
Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
oXMLHttp.open cMethod, cAPIURL, False, cUser, cPassword
oXMLHttp.setRequestHeader "Content-Type", "application/json"
oXMLHttp.send(cData)
cReturn = oXMLHttp.responseText
Set oXMLHttp = Nothing
The service I'm calling expects the content-type to be application/json
, naturally. As you can see, I'm setting the request header above to be as such.
The issue is that MSXML2.ServerXMLHTTP
will append a charset to whatever I set the content-type to (I can't find documentation for this behavior), with the default appearing to be UTF-8. So in the end, the header is sent as application/json; Charset=UTF-8
, which the web service does not like.
Odd thing is, I can explicitly set a charset with setRequestHeader
, even a nonsensical one, and MSXML2.ServerXMLHTTP
will then leave the header alone. For example..
oXMLHttp.setRequestHeader "Content-Type", "application/json; Charset=FOO"
Works fine and is left untouched. How can I stop MSXML2.ServerXMLHTTP
from altering the content-type?
EDIT: I found that MSXML2.ServerXMLHTTP 6.0 does not exhibit this behavior, at least by default. But I'd still like to see if there's a solution for this, as I'm not sure if that'll be available where this application needs to be installed.
Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
According to the docs, this is an expected behavior of send method when the request body is string as yours. BTW I should say I can't reproduce the issue on Windows 10 and Windows Server 2008 R2 but Windows Server 2008. So the only thing I can say this behaviour must be valid for older builds of MSXML 3.0.
Remarks
As a workaround, you can send bytes of
cData
instead of pointing the variable. This will work for all.