How to make Siddhi basic authentication work - http

I'm attempting to setup a simple Siddhi flow where I receive a message using an http listener, use a field from that message as an input to an http API call, then log the result.
The problem I'm having is I cannot seem to get basic http authentication to work on the API call. It seems to just not send an Authorization header. Here is the Siddhi flow I built:
#App:name('Test Flow')
#App:description('Listen, call, log')
#source(type = 'http', receiver.url = "http://0.0.0.0:8007/myListener",
#map(type = 'json',
#attributes(someInputField = "$.someInputField")))
#sink(type = 'http-call', publisher.url = "https://somehost.com/someApiPath", sink.id = "callAPI.sink", basic.auth.username = "testUser", basic.auth.password = "testPass", https.truststore.file = "/usr/local/ssl/keystore.jks", https.truststore.password = "someTruststorePass", headers = "'Content-Type: application/json'", method = "POST", hostname.verification.enabled = "true",
#map(type = 'json',
#payload(""" { "someStandardPayloadField: "abc", "someInputField": "{{someInputField}}" } """)))
define stream APIStream (someInputField string);
#source(type = 'http-call-response', sink.id = "callAPI.sink", http.status.code = "200",
#map(type = 'json', enclosing.element = "$.outputs",
#attributes(someResponseField = "someOutputfield")))
#sink(type = 'log', prefix = "LOGGER")
define stream logStream (someResponseField string);

Related

Aws SNS for sending Messages Status 'waitingforActivation'

I am using aws sns for sending message, but when i am testing on console application it return waitingforActivation in status.
AWSConfigs.AWSRegion = "me-south-1";
AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(AWSAPIKey, AWSSecretKey);
PublishRequest pubrequest = new PublishRequest();
pubrequest.Message = "AWS API SMS";
pubrequest.PhoneNumber = "mobile number of Receiver";
pubrequest.MessageAttributes.Add("AWS.SNS.SMS.SNSType", new MessageAttributeValue { StringValue = "Transational", DataType = "String" });
var response = snsClient.PublishAsync(pubrequest);*

WooCommerce REST API with Google UrlFetchApp header authorization not working

CURRENTLY
I am utilising WooCommerce REST API in my Google Scripts with the following working code:
var ck = "ck_longstringlongstringlongstringlongstring";
var cs = "cs_longstringlongstringlongstringlongstring";
var website = "https://www.mywebsite.com.au";
var url = website + "/wp-json/wc/v3/orders?consumer_key=" + ck + "&consumer_secret=" + cs;
var options =
{
"method": "GET",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"muteHttpExceptions": true,
};
var result = UrlFetchApp.fetch(url, options);
PROBLEM
To improve security, I want to put consumer key and secret into the header, but I cannot get the following script to work
var url = website + "/wp-json/wc/v3/orders;
let authHeader = 'Basic ' + Utilities.base64Encode(ck + ':' + cs);
var options =
{
"method": "GET",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"muteHttpExceptions": true,
"headers": {"Authorization": authHeader},
};
var result = UrlFetchApp.fetch(url, options);
Current result = {"code":"woocommerce_rest_cannot_view","message":"Sorry, you cannot list resources.","data":{"status":401}}
Expected result = JSON of orders
Is it an issue with my code? Or with WooCommerce API or Google Scripts?
There are a few issues with your code.
With UrlFetchApp.fetch() you need to use contentType instead of Content-Type.
However, in this case you don't even need to set it since application/x-www-form-urlencoded is the default. Same goes for the method property; it defaults to GET.
Moreover, if you want to err on the side of caution use Utilities.base64EncodeWebSafe(data, charset) to base64 encode your credentials instead of the non-web-safe version.
let url = website + "/wp-json/wc/v3/orders";
let encoded = Utilities.base64EncodeWebSafe(ck + ':' + cs, Utilities.Charset.UTF_8);
let options = {
"muteHttpExceptions":true,
"headers": {
"Authorization": "Basic " + encoded
}
};
let result = UrlFetchApp.fetch(url, options);
result = JSON.parse(result);

Lua: How to parse http post result?

I am writing a http client in lua to consume a rest web service.
The webservice result should be in this json format:
{
"code": "123456789",
"valid_until": "09/09/2020"
}
This is my lua script:
param_1 = arg[1];
param_2 = arg[2];
http = require("socket.http");
ltn12 = require("ltn12");
path = "http://www.url.com:8080/api/";
body = [[ {"number_id":"b8ce37fb-2061-4aea-975b-57a8e2d355ce","param_1":"]]..param_1..[[","param_2":"]]..param_2..[["} ]];
response_body = { }
res, code, response_haders, status = http.request {
url = path,
method = "POST",
headers =
{
["X-Access-Key"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
["Content-Type"] = "application/json",
["Content-Length"] = body:len()
},
source = ltn12.source.string(body),
}
print("res:"..res);
print("code:"..code);
print("status:"..status);
when I run my script this is the results I got:
res:1
code:201
statusHTTP/1.0 201 CREATED
Why I getting 1 as result, What should I do to parse the result?

Yobit API POST request (R)

I try to request to yobit API wthin R.
To get access to some of the methods you need to complete authentication:
Each Trade API request should pass authentication.
Authentication is fulfilled by sending the following HTTP-titles:
Key - API-key, example: FAF816D16FFDFBD1D46EEF5D5B10D8A2
Sign - digital signature, POST-parameters (?param0=val0 & ...& nonce=1) signed by secret key through HMAC-SHA512
Parameter nonce (1 minimum to 2147483646 maximum) in succeeding request should exceed that in the previous one. To null nonce it is necessary to generate new key.
My code :
nonce=1
API_KEY = "0B02AD5AF57854184D68D3D1D4D980F9"
API_SECRET = "89b20f882220b5dc6feeb33253c25ba3"
Body=paste('method=getInfo&nonce=',nonce, sep="")
sign = hmac(API_SECRET, Body, algo="sha512")
title=add_headers('Content-type'='application/x-www-form-urlencoded', Key = API_KEY, Sign = sign)
rep=POST('https://yobit.net/tapi', body=Body, headers=title, encode='form')
nonce=nonce+1
Response from server:
"{\"success\":0,\"error\":\"invalid key, sign, method or nonce\"}"
Thanks for help!
this i have done in node js And its working.
const crypto = require("crypto");
var apikey = 'apikey';
var secret = 'secret';
var signature = "method=getInfo&nonce="+ nonce;
console.log(signature);
var hmacsignature = crypto.createHmac('sha512', secret).update( signature ).digest('hex');
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Key': apikey,
'Sign': hmacsignature
};
var options = {
url: 'https://yobit.net/tapi',
method: 'POST',
body: signature,
headers: headers
}
console.log(options);
request1(options, function (error, response, body) {
res.send(body);
});

Why am I getting 'invlid-api-key' from AlchemyAPI while using HTTP.call in meteorjs?

I am trying to get the sentiment of a piece of text using th AlchemyAPI in my meteor application. I am using HTTP.call with 'POST' as recommended by the API to make a server to server call, but I am getting an 'invalid-api-key' response.
var alchemyURL = Meteor.settings.alchemyUrl;
var postData = {
'apikey': Meteor.settings.alchemyUrl,
'text': txt,
'outputMode': 'json'
};
var options = {
data: postData
};
var sentimentData = HTTP.call('POST', alchemyURL, options);
console.log(sentimentData);
I have discovered the answer, to this hence posting it in below.
So turns out, Meteor's HTTP package needs to be given the headers for implementing form url-encoding on the data. Also the data object needs to be passed in 'params' and not in 'data' The correct snippet to be used is given below.
var alchemyURL = Meteor.settings.alchemyUrl;
var postData = {
'apikey': Meteor.settings.alchemyUrl,
'text': txt,
'outputMode': 'json'
};
var options = {
params: postData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var sentimentData = HTTP.call('POST', alchemyURL, options);
console.log(sentimentData);

Resources