Unable to retrieve data from client via httpRequest
I have a client that no longer wants to use sftp to transmit their data file to me. Instead they want me to pick it up via a web service.
Email from client:
Here is the postman collection and mocking service to start your development. The API has only one URI parameter {id} for which you need to pass UniqueIdentifier(will let you know exact value later).
attachments: ssrg-exp-api.postman_collection.json, IB006_Response.json
- The attachments the client provided can be found in the attached pdf KCS API documents.pdf
When going to the link supplied via a web browser ( {id} should be part of the link ), I see the results desired:
But when I use my code from within Cache, I'm getting html code:
SRG>d GET^KCSAPI
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Added IE-Edge to fix login rendering in Studio -->
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>Anypoint Platform</title>
<link rel="shortcut icon" href="https://cdn.anypoint.mulesoft.com/core/v1/icons/favicon.ico">
<!-- Preload the icons for the navbar to improve render speed and ensure that they are properly cached. -->
<link rel="prefetch" href="/icons/sprite-4.15.1.svg" type="image/svg+xml">
<!-- Preload the font for the navbar so that it renders immediately on login -->
<link rel="preload" href="https://cdn.anypoint.mulesoft.com/artifacts/anypoint-styles/fonts/DINPr…" as="font" crossorigin>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="referrer" content="origin">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<link href="https://cdn.anypoint.mulesoft.com/artifacts/anypoint-platform-ui/app/st…" rel="stylesheet">
<meta name="ui-version" content="v0.22.0-1-g3896105">
</head>
<body class="anypoint-styles">
<script>
window.Anypoint = {
deployment: 'cloud'
};
if (window.Anypoint.deployment === 'cloud') {
// We only load analytics for the cloud environment, not for PCE
const analyticsScript = document.createElement('script');
analyticsScript.src = '/shared/analytics.min.js';
document.body.appendChild(analyticsScript);
}
</script>
<div id="shellRoot"></div>
<!-- core-js polyfills for IE11 support -->
<script src="https://cdn.anypoint.mulesoft.com/artifacts/anypoint-platform-ui/app/co…"></script>
<script src="https://cdn.anypoint.mulesoft.com/artifacts/anypoint-platform-ui/app/re…"></script>
<script src="https://cdn.anypoint.mulesoft.com/artifacts/anypoint-platform-ui/app/re…"></script>
<script src="/shared/anypoint-navbar.js"></script>
<script type="text/javascript" src="https://cdn.anypoint.mulesoft.com/artifacts/anypoint-platform-ui/app/bu…"></script>
</body>
</html>
Cache code:
K S $ZT="^%ET"
s id="{id}"
set httprequest = ##class(%Net.HttpRequest).%New()
set httprequest.SSLConfiguration = "HTTPS"
set httprequest.Server = "www.anypoint.mulesoft.com"
set httprequest.Location = "/mocking/api/v1/links/03a74865-d7fb-4318-a310-7ad2b95361f6/v1/receivables/"_id
set httprequest.Https = 1
set httprequest.ContentType="json"
set sc=httprequest.SetHeader("Connection","close")
set status = httprequest.Get("/") ;returns html page
//s status = httprequest.Get("") ;returns msg: {"code":"INVALID_RESPONSE_ACCEPT_HEADER","message":"Error trying to find response for invalid media type"}
//set status = httprequest.Get("anypoint.mulesoft.com/mocking/api/v1/links/03a74865-d7fb-4318-a310-7ad2b95361f6/v1/receivables/9") ;commented out httprequest.Location - returns same html page
if status {
s response=httprequest.HttpResponse //IS JUST RETURNING PAGE
do response.Data.Rewind()
do {
write response.Data.Read()
} while 'response.Data.AtEnd
}
quit
Can someone point me in the right direction and/or let me know where the error lies in my code. Why am I not able to pull the desired results? (as shown in the screen snippet).
Comments
Try this way
K S $ZT="^%ET"
s id="{id}"
set httprequest = ##class(%Net.HttpRequest).%New()
set httprequest.SSLConfiguration = "HTTPS"
set httprequest.Server = "anypoint.mulesoft.com" /// CHANGED
set httprequest.Https = 1
set httprequest.ContentType="json"
set sc=httprequest.SetHeader("Connection","close")
set status = httprequest.Get("/mocking/api/v1/links/03a74865-d7fb-4318-a310-7ad2b95361f6/v1/receivables/"_id") /// CHANGED
if status {
s response=httprequest.HttpResponse //IS JUST RETURNING PAGE
do response.Data.Rewind()
do {
write response.Data.Read()
} while 'response.Data.AtEnd
}
quit
That gives me the following:
D GET^KCSAPI
{"code":"INVALID_RESPONSE_ACCEPT_HEADER","message":"Error trying to find response for invalid media type"}
I added an output of the headers:
SRG>D GET^KCSAPI
HTTP/1.1 415 Unsupported Media Type
CONNECTION: Close
CONTENT-LENGTH: 106
CONTENT-SECURITY-POLICY: sandbox;
CONTENT-TYPE: application/json
DATE: Wed, 07 Jul 2021 15:38:26 GMT
MS2-ERROR: MockRequestError
SERVER: nginx
STRICT-TRANSPORT-SECURITY: max-age=31536000; includeSubDomains
X-CONTENT-SECURITY-POLICY: sandbox;
X-ENVOY-DECORATOR-OPERATION: service.mocking.svc.cluster.local:80/*
X-ENVOY-UPSTREAM-SERVICE-TIME: 9
X-REQUEST-ID: 7deac252-b38a-430e-9931-e77a8b87bef2
Error has occurred <COMMAND> in SRG at 11:38 AM Jul 07 2021
(now saving error/stack information)
Any documentation I can find containing this 'error' refers to making POSTs. I'm doing a GET, so I'm completely baffled....
I think based on your error it doesn't like
set httprequest.ContentType="json"
Which your browser based version is probably not setting.
That wasn't it. I have commented that line out completely, and I get the same error. (415 Unsupported Media type).
I also changed it from json to application/json (as indicated in the headers retrieved from the server) - Still the same error.
First the content type is not correct. This should be application/json. Next I would add the following statement
do httprequest.SetHeader("Accept","application/json")
This tells the request what type of response your application will accept. The default is text/html I believe which can't be supplied by the api.
Thank you so much!!! Setting the header accept was what was needed.