TimeGenerated field not taking provided date - azure-log-analytics

I am writting custom logs to log analytics.
Based on the following link:
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/data-collector-api#create-a-request
I should be able to use a field in the request header called: time-generated-field, the documentation says that: " If you specify a field, its contents are used for TimeGenerated. If you don't specify this field, the default for TimeGenerated is the time that the message is ingested. The contents of the message field should follow the ISO 8601 format YYYY-MM-DDThh:mm:ssZ"
I am passing the following value: 2021-11-11T19:52:45Z (as a string, since you can't pass this as a datetime object) but the problem is that when I look in the log analytics workspace, the TimeGenerated field is this (today's date): 2021-12-01T18:41:04.529Z which is the datetime the event is ingested, so basically, it's not taking the real event generated time which is 2021-11-11T19:52:45Z which I am passing in the header.
Am I doing something wrong here?
Any help would be appreciated, I am running out of ideas here.

It turns out I misread the documentation and didn't realize that the time-generated-field is not an argument you pass in, but a property in the body of the JSON data that you are sending to the log analytics workspace.
It can be referenced in this manner (this is an example of 1 way of doing it):
def post_data(customer_id, shared_key, body, log_type):
method = 'POST'
#The string "raised" assigned to the TimeStampField variable below
#is an actual property in the JSON object that I am sending to log
#analytics, it contains the datetime in the expected format.
TimeStampField = "raised"
content_type = 'application/json'
resource = '/api/logs'
rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
content_length = len(body)
signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource)
uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01'
headers = {
'content-type': content_type,
'Authorization': signature,
'Log-Type': log_type,
'x-ms-date': rfc1123date,
'time-generated-field': TimeStampField
}
Will definitely create a pull request to modify the wording here:
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/data-collector-api#create-a-request
As I feel it is not very intuitive

Related

Unable to pass the value from a variable/JSON file in the payload

I am trying to pass a value inside below payload, I am able to get the value of it from the print but same is not accessible inside the payload, It prints the #(config.Secondary_ED) as the output.
The config.Secondary_ED contains an ip address. It works fine when I give ip address manually.
And print config.Secondary_ED
And request {"agent-forwarding":true,"configuration-data":"{\"service-host\":\"#(config.Secondary_ED)\",\"service-port\":\"8443\",\"user\":\"restdispatcher\",\"protocol\":\"vosrestdispatcher:rest\"}","configuration-template":null,"description":"Change Guardian Default Event Destination sk-12sp5","display-name":"sk-12sp5","ev-prototype-id":"1","forwarding-queries":[""],"id":1,"is-default":true,"is-indelible":true,"method":"vosrestdispatcher:rest","model":"REST Dispatcher","server-fordwarding":false}
When method PUT
Working Payload
#And print config.Secondary_ED
And request {"agent-forwarding":true,"configuration-data":"{\"service-host\":\"1.1.1.1\",\"service-port\":\"8443\",\"user\":\"restdispatcher\",\"protocol\":\"vosrestdispatcher:rest\"}","configuration-template":null,"description":"Change Guardian Default Event Destination sk-12sp5","display-name":"sk-12sp5","ev-prototype-id":"1","forwarding-queries":[""],"id":1,"is-default":true,"is-indelible":true,"method":"vosrestdispatcher:rest","model":"REST Dispatcher","server-fordwarding":false}
**Secondary_ED is stored in a json file called config.js
I have used # many times to get the value but same doesn't work in this case.
Please suggest what did I miss here.
Appreciate your help.
Please notice that the value of configuration-data is a string, not JSON object. Ask a friend who knows JS if this is not clear.
You can try this (note the usage of string):
* string configData = { 'service-host': '#(config.Secondary_ED)' }
* request { foo: '#configData' }
Or just use JS string concatenation (this is just a guess, please figure out what works for you, but you get the idea.
* def configData = '"{\\"service-host\\":\\"' + config.Secondary_ED + '\\"'

Karate / Post Request : Unable to pass random generator variable in json payload

I am trying to create Karate feature file for API Testing.
It is a post request and we have to pass random number for 1 field i.e. order in request payload every time.
Now, I am trying to use this function in the same feature file to pass that random generated value in json payload before sending post request.
Could someone please have a look at my feature file and help.
Thanks
Also, Is there a way if i want to pass this same random value for a json payload created as a separate request.json payload file
Your requestPayload is within double quotes, so it became a string.
Here's an example that should get you going. Just paste it into a new Scenario and run it and see what happens.
* def temp1 = 'bar'
* url 'https://httpbin.org/anything'
* def payload = { foo: '#(temp1)' }
* request payload
* method post
And please read the documentation and examples, it will save a you a lot of time.

Call to java script code returning the ASCII encoding for ':' separating key and value of returned object

I making an api request using karate where one of the api request params takes a filter condition (which is a java script object).
I am using a literal notation to create a java script object as shown below. This code is in a separate filter.js file.
function() {
var params = {
val1:[],
val2:[]
};
return params;
}
Now i call the above .js file in the karate scenario as below:
Scenario: Test
Given path 'filtertest/'
* param filter = call read('classpath:feature/common/filter/filter.js')
When method get
Ran the above and when i check the log, the api throws bad request error. I looked at the request url and there i can see that the ':' in the js file where I am assigning a value to a object key is replaced with %3A which i believe is the ASCII encoding for ':'. (the param with its values below)
?filter=%7B%22val1%22%3A%5B%5D%2C%22val2%22%3A%5B%5D
What I want is the ':' to come as it is from the .js call as the server side expects the filter param values as key value pairs.
Is there a way I can achieve this?
If your server cannot decode an encoded : it is a bug: https://www.w3schools.com/tags/ref_urlencode.asp
If you really need this - the workaround is to use the url keyword and build it manually, path and param will always encode.
Given url baseUrl + '/filtertest?filter=foo:bar'

How to pas ISO 8601 formatted date value in GET Request

I am working on lob.com letters API. Using Postman application to test the API, facing the following error:
Postman Application to test Lob's API
The parameter "date_created" want date value in ISO 8601. I am passing the value in specified format but api returns the response having error: "date_created must be an object".
Need help!
In pre request script compute current date and set it to environment variable then you can pass it in your request body. A bit of code will look like this:
Pre Request
var date = new Date().toISOString();
postman.setEnvironmentVariable("date",date);
Request Body
// Other attribs....
"date_created" : "{{date}}"

ShopStyle API - How to Invoke an HTTP request

How do I Invoke an HTTP request with a particular URL and process the body of the response as XML?
Information Provided by ShopStyle:
HOW TO USE THE API:
Choose the method that returns the data your application needs. For example, the /products method is used to get products that match a given category or brand.Construct a URL for that method with the appropriate host, method name, and query parameters. Invoke the URL as an HTTP GET.
When the HTTP response arrives, extract the required data elements from the response's body.The rest of this document describes the details of constructing the right URL for each of the API methods. The XML format of the responses may be seen by clicking on the sample URLs shown for each method. The responses in JSON format contain identical information, just in a different format.
SHOPSTYLE API URLS
All ShopStyle API URLs have the following form:
http://api.shopstyle.com/api/VERSION/METHOD_NAMEpid=YOUR_API_KEY&format=FORMAT&...
The METHOD_NAME is taken from the list of methods in the various API shown at left (Press Link To View List of Methods-https://www.shopstylecollective.com/api/overview).
COMMON API PARAMETERS
All methods in the API accept these parameters:
API_KEY (my unique key is ******************)
pid Unique API_KEY string that is assigned to the caller. You can find your API Key on the Account Settings page.
FORMAT
The format of the response. Supported values are:
json - The response is in JSON format with UTF-8 encoding. This is the default if the parameter is absent.
xml - The response is in XML format with UTF-8 encoding.
jsonp - The response is in JSON format with UTF-8 encoding wrapped in a JavaScript method called padding. The padding must be specified with the query parameter 'callback'. Only single expressions (function reference, or object property function reference) are accepted as valid padding.
When set to 1 or 'true' the HTTP status will always be 200. Use the field "errorCode" in the response to detect whether the API Call was successful. By default, when an error occur the HTTP Status of the response will be different than 200
Again I am a beginner, so please provide detailed information on how to retrieve CATEGORY data (Examples: Dresses, Tops, Buttoms, etc) in XML format.**
Thank you!!!
Here's a simple example to get your started. Copy the code below and put it into a file, say "cat.php". Then run it by typing "php cat.php" at a command prompt (assumes you have php on your machine):
<?php
// don't show dom parse errors
libxml_use_internal_errors(true);
// grab the xml from the api
$response = file_get_contents("http://api.shopstyle.com/api/v2/categories?pid=TEST&format=xml");
$doc = new DOMDocument();
$doc->loadHTML($response);
// grab all the categories
$elements = $doc->getElementsByTagName('categories');
foreach($elements as $node){
foreach($node->childNodes as $child) {
// get the name out of the category
$nodes = $child->getElementsByTagName("name");
foreach ($nodes as $name) {
echo $name->nodeValue . PHP_EOL;
}
}
}