Passing a (timestamp) as an argument for a REST GET API - javascript

I have been trying to get resources using GET Request. You can see the actual Query here:
{ "success": true,"data": [
{
"ID": 123
"Code": "Test",
"Enabled": true,
"Flags": 0,
"Niveau": 0,
"SQL": "SELECT GEMEENTE.GM_ID,\r\n GEMEENTE.GM_POSTNUMMER,\r\n
GEMEENTE.GM_DEELGEMEENTE,\r\n GEMEENTE.GM_LAND_FK,\r\n
LAND.LN_ACTIEF\r\nFROM GEMEENTE\r\n LEFT JOIN LAND ON GEMEENTE.GM_LAND_FK =
LAND.LN_ID\r\nWhere GM_VERANDERDOP >=:ChangedOn",
"Parameters": [
"ChangedOn"
],
"QueryType": 0
}]}
However I need to pass an Argument to the "Parameters" property so i can actually see the response data. I see "ChangedOn" is a the argument array I need to set to a date. I tried this: http://localhost/A/B/server.fcgi/Query?Code=Test&Parameters="2015-07-05T22:16:18Z"
I am working with javascript and vue.

What are we looking at here? Is this the server code of your API. In that case, it looks like you need to supply a ChangedOn parameter. Also, your query string should be url encoded. Try calling...
http://localhost/A/B/server.fcgi/Query?Code=Test&ChangedOn=22015-07-05T22%3A16%3A18Z

Related

JavaScript convert JavaScript object to JSON object

What is the shortest method to convert a JavaScript object to a JSON object? The beneath is my JavaScript object.
{
"body": [
{
"id": "1",
"action": "alert",
"activityGroupNames": "test2"
},
{
"id": "2",
"action": "alert",
"activityGroupNames": "test3"
},
{
"id": "3",
"action": "alert",
"activityGroupNames": "test2"
}
]
}
I making use of an inline code script in Microsoft automate that performs the following in JavaScript:
var threat = workflowContext.actions.Compose.outputs;
var value = Object.values(threat);
return value;
I am supposed to HTTP POST a JSON object in an API request, however, I am submitting a JavaScript object type and am unsure as to how I can change this. Any help would be greatly appreciated! Please let me know if you require any further context.
Edit: The HTTP POST request is failing due to "Object reference not set to an instance of an object"
Javascript provides with built-in method JSON.stringify() It will help you to convert JavaScript object into JSON and pass through HTTP Request
console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"
A common use of JSON is to exchange data to/from a web server.
When receiving data from a web server, the data is always a string.
Parse the data with JSON.parse(), and the data becomes a JavaScript object.
const json = '{ "fruit": "pineapple", "fingers": 10 }';
const obj = JSON.parse(json);
console.log(obj.fruit, obj.fingers);

node.js correct extending of json objects

I'm currently using node-ews in a project to access an Exchange Web Server via node.js. Now i ran into a weird problem. To run for example a "CreateItem" request, which can be an E-Mail or Appointment for example, I'm giving the function the arguments as a json similar to this
var args = {
"attributes" : {
"SendMeetingInvitations" : "SendToAllAndSaveCopy"
},
"SavedItemFolderId": {
"DistinguishedFolderId": {
"attributes": {
"Id": "calendar"
}
}
},
"Items" : {
"CalendarItem" : {
"Subject" : "",
"Body" : {
"attributes" : {
},
"$value" : ""
},
"ReminderIsSet" : "true",
"ReminderMinutesBeforeStart" : "30",
"Start" : "",
"End" : "",
"IsAllDayEvent" : "false",
"LegacyFreeBusyStatus" : "Busy",
"Location" : ""
}
}
};
As the REST-API I'm writing will receive Attributes like Subject, Start, End etc. I initially stripped out those out of the JSON and would define them later like
args.Items.CalendarItem.Subject = req.body.Subject;
Oddly this will make the internal validation of node-ews fail and tell me that CalendarItem has an invalid Child Subject. If i leave Subject als an empty string in the initial args and later change it set it to req.body.Subject it works just fine.
My question is this: Is the Object somewhat different if I later add attributes and if yes is there a way to do it right? Because I don't think its the best way to have a bunch of empty Attributes in my Object if they aren't used and define standard values for all of them even if api won't require them to be sent.
Would great if someone knew the answer. Hope i could clarify what the problem is
Ok,
so the problem seems to be this. Internally the JSON "object" seems to have an order based on the order the variable is defined. in JavaScript this is no problem. But when the xml is parsed, the tag that was defined last will also be at the end of the xml so if you had
<someTag>
<variable2 />
<variable3 />
</someTag>
and you add variable1 to your json by someTag.variable1 = x in the end the xml will look like this after beeing parsed by node-ews
<someTag>
<variable2 />
<variable3 />
<variable1 >x</variable1>
</someTag>
Now unfortunately the exchange web server seems to be picky about the order of the xml tags. so when you build your json be sure to use the direct order. Changing content of the json later will not affect the order.

JSON parsing and stringify

I am currently working on a Javascript project where I have to parse tons of data. The project requires that I parse some JSON data and bring specific data into another array. Right now, I am using the JSON.stringify method and I can console.log all of the data that I need. The data looks something like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-85.3865810000125,
33.90171899971196
],
[
-85.38659500025622,
33.9017919996593
],
yes, this is not all that data-it is about 1200 pages long! I only pasted in the top segment of it. All I really need is how to get to the coordinates aspect of it and into an array. So what I am currently doing is this:
var work = JSON.stringify(response, null, 4)
console.log(work);
Which gives me the above response. However, I am not that familiar with JSON.stringify so if I do:
console.log(work.type);
Attempting to see what the value is for type, I get a response of undefined. Now if I try doing this:
var work = JSON.parse(response)
console.log(work);
I get a response of: VM296:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
Thus, I cannot get the data parsed which I want to get to which will be the coordinates data. Any help with using stringify would great help me. I have read a lot about how it turns the data into a string but have not really seen a lot about how to parse it. Do I have to use parse? Thank you for your help!
You dont' have to use stringify or parse for this. The response object you are receiving is already a javascript object so you can reference its properties normally
response.type; // => "FeatureCollection"
If you try and stringify the response it will lose its type property, for instance:
typeof "some string".someProperty; // => undefined

Javascript promises fetching json data

I am trying to learn promises in javascript and I came across this short video tutorial, there is a complete code from it on plunker.
What I wonder about in this code is in this line in promises.js file:
return $.get('tweets.json?id=' + profile.id);
First of all, why do we even need to pass any parameters there, since there is no profile.id field in tweets.json file, and when removing it I get the same results. If there was that field in tweets.json how would we achieve to get only tweets from the users that we pass with get request?
I have tried something like this but it doesn't work:
In promises.json I have changed the get request to:
return $.get('tweets.json?profile=' + profile.id);
And have added a new field to the tweets.json file, but only to the first tweet:
{
"id": 1,
"profile": 12302151,
"tweet": "OMG, worst day ever, my BF #BobbyBoo dumped me",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
},
I was then expecting that I will only get that one tweet for that user, but I am still getting the whole list

How to access data within json using nodejs

I'm trying to access data within a json file using nodeJS
When I run this I get the error : TypeError: Cannot read property 'postcode' of undefined. Any Suggestions?
{
"apiName": "Restaurants",
"pages": [
{
"pageUrl": "https://url",
"results": [
{
"address": "3F Belvedere Road Coutry Hall, London, SE17GQ",
"phone": "+442076339309",
"name": "Troia",
"postcode": "SE17GQ"
}
]
}
]
}
var myData = require('./jsonFile.json');
console.log(myData.pages.result.postcode);
Try to access data as below:
console.log(myData.pages[0].results[0].postcode);
The value in the bracket is the index of element to access.
Its the common singular/plural trap, I fall for it all the time.
In your json, pages & results are arrays. You need to access these with an index. Also, you have a typo in the name.
Try this:
console.log(myData.pages[0].results[0].postcode);
This will gave you correct answer.
console.log(myData.pages[0].results[0].postcode);

Categories