yes, thanks for the code. I don’t see what response you’re getting, or what you’re expecting to get.
One thing I’ll point out. This code section looks wrong
var SchCodeRequest = '{ "aa" : "","abb" : ""}';
var req = JSON.parse((JSON.stringify(SchCodeRequest)));
… in a couple ways.
First, invoking JSON.parse() on the output of JSON.stringify() is basically a no-op, if I’m not mistaken. They are inverse functions, so you will just get the original argument out.
foo == JSON.parse(JSON.stringify(foo)) // always true
So it seems pointless to stringify, then parse the SchCodeRequest thing in your 2nd line.
Getting to the intent of your code, It seems like you want the request payload to be json. In that case, I suggest you initialize a JS object, and then serialize it with JSON.stringify.
var SchCodeRequest = { "aa" : "","abb" : ""}; // no single quotes wrapping this
var payload = JSON.stringify(SchCodeRequest);
Your code has
var SchCodeRequest = '{ "aa" : "","abb" : ""}'; // this is a string
The right-hand-side is wrapped in single quotes. That means it’s a string. The string just happens to have somethiing that looks like JSON within it. But it’s still a string.
On the other hand, this version without quotes:
var SchCodeRequest = { "aa" : "","abb" : ""}; // this is an object
…initializes an object. It has a couple properties, aa and abb.
If you then call JSON.stringify() on that object you will get … a string… similar to the quoted string in your original code.
ok, aside from that payload detail, let me say that I am not at all clear on the problem you’re experiencing. I think the output you’re seeing is not what you’re expecting, but I don’t see examples of either of those in your post.
But I want to point out something else. You’re trying to invoke “https://localhost”. What is your intent with that? Maybe you have a server running on your local developer workstation and you want an Apigee JS policy to contact that server? If that’s your intent, it won’t work. The Apigee JS policy runs on the message processor machine, which is in the Apigee cloud. (1) It won’t have connectivity to your local development workstation, and (2) it won’t be able to resolve “localhost” to your workstation even if connectivity WAS possible.
So you may need to rethink what you’re trying there.
One final recommendation: use the onComplete callback for the HTTP client.
function onComplete(response, error) {
if (response) {
context.setVariable('example.status', response.status);
// response.content here holds the body
}
else {
context.setVariable('example.error', 'Whoops: ' + error);
}
}
var url = 'https://whatever.example.com/foo/bar'; // not localhost!
var payload = JSON.stringify({
foo : 'bar',
whatever : 1234
});
var headers = {
'Content-Type' : 'application/json',
'Authorization' : 'Bearer xyz'
};
var req = new Request(url, 'POST', headers, payload);
httpClient.send(req, onComplete);
Good luck on your explorations.