'Python REST call to VB
First - I admit I'm in over my head on this. I know how do do SOAP calls, but this REST stuff is all new to me.
I have a Python script that calls our company CRM and it's working as it should. I need to convert this over to VB6 (or even VB .net), but I can't seem to figure out just how to get the thing to work. I know I'm talking to the CRM, but it keeps giving me an error (probably because I'm sending wrong data).
Here's the working Python Script (NOTE: the authtoken variable comes from another script that's working fine):
fetchurl = https://URL-TO-THE-REST-FUNCTION
searchheaders = {
'accept': 'application/json; charset=utf-8',
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Bearer ' + authtoken
}
payloadstring = {
"request": {
"companyNumber": 1,
"operatorInit": "user",
"operatorPassword": "Password",
"inputString": "TEST_STRING"
}
}
payload = json.dumps(payloadstring)
fetchresponse = requests.post(fetchurl,
headers=searchheaders,
data=payload,
verify=False)
apiResponse = fetchresponse.content
print(json.loads(apiResponse))
And here's what I have so far in VB6
Dim sURL As String
Dim strXML As String
Dim xmlhttp As MSXML2.xmlhttp
Set xmlhttp = New xmlhttp
Open "D:\API\token.dat" For Input As #1
Line Input #1, token$
Close #1
Data$ = "'{" & Chr$(34) & "request" & Chr$(34) & ": {"
Data$ = Data$ & Chr$(34) & "companyNumber" & Chr$(34) & ": 1,"
Data$ = Data$ & Chr$(34) & "operatorInit" & Chr$(34) & ": " & Chr$(34) & "user" & Chr$(34) & ","
Data$ = Data$ & Chr$(34) & "operatorPassword" & Chr$(34) & ": " & Chr$(34) & "password" & Chr$(34) & ","
Data$ = Data$ & Chr$(34) & "inputString" & Chr$(34) & ": " & Chr$(34) & "Work, Damnit!" & Chr$(34) & "}}'"
sURL = "https://URL-TO-The-REST-FUNCTION"
strXML = Data$
xmlhttp.open "POST", sURL, True
xmlhttp.setRequestHeader "'accept':", "'application/json; charset=utf-8'"
xmlhttp.setRequestHeader "'Content-Type':", "'application/json;charset=utf-8'"
xmlhttp.setRequestHeader "'Authorization': 'Barer '", token$ & "'"
xmlhttp.send strXML
The Data$ looks like this:
'{"request": {"companyNumber": 1,"operatorInit": "user","operatorPassword": "password","inputString": "Work, Damnit!"}}'
I know I'm at least talking to the server, the returned header looks like this:
Date: Fri, 04 Mar 2022 20:28:03 GMT
Content-Type: application/json
Content-Length: 24
Connection: keep-alive
x-frame-options: SAMEORIGIN
x-transaction-id: 5dfcb31d-9d85-94ce-b6cd-81416aaab4c2
strict-transport-security: max-age=31536000; includeSubDomains
www-authenticate: Bearer realm="IONAPI"
server: ionapi-2022.02.00.995424
x-envoy-decorator-operation: ionapi_gateway
And the response I'm getting is
{"error":"Unauthorized"}
(I know the username/password I'm plugging in are good)
Any help would be appreciated. Thanks
Solution 1:[1]
Let me clear those headers for you
xmlhttp.setRequestHeader "Accept", "application/json"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.setRequestHeader "Authorization", "Barer " & token$
There is no ' in the header name, there is no : in the header name, there is no ' in the header value too.
The Authorization value is Bearer {token_here}, so Bearer is not part of the header name but the header value.
There is no need to specify charset for application/json content-type as this is always in utf-8 and no other charset is supported by definition (this is not XML where you can have various charsets).
JFYI, header Content-Type means your strXML contains JSON i.e. this is the content of the body you are posting.
Header Accept means you expect JSON from the server to be returned. Try omitting this for brevity if your endpoint defaults to JSON output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wqw |
