I am using the data source http to perform GET requests on the FreshService API in Terraform
My code is as follows:
# Query FreshService
data "http" "example" {
url = ".../api/v2/changes/7195/tasks"
# Optional request headers
request_headers = {
Accept = "application/json"
Authorization = "XXXXXXXX"
}
}
output "freshservice_task_title" {
# Gets the root block volume id
value = "Targeting Volume ID: ${jsonencode(data.http.example.body)}"
}
The output is returning something like this:
{
"tasks": [
{
"id": XXXX,
"agent_id": XXXX,
"status": 3,
"due_date": "2021-04-07T09:30:00Z",
"notify_before": 0,
"title": "XXXXX",
"description": "",
"created_at": "2021-03-31T12:28:43Z",
"updated_at": "2021-04-07T12:43:55Z",
"closed_at": "2021-04-07T12:43:55Z",
"group_id": XXXXX
}
]
}
I need to iterate the tasks in Terraform to do some manipulation but i am not able to do so.
I tried toset(data.http.example.body.tasks) and tolist(data.http.example.body.tasks) but it says attribute doesn't exists.
Can someone please help?
The docs say that the body is raw response. I So I think you should use jsondecode, not jsonencode. Then you access tasks as follows:
jsondecode(data.http.example.body).tasks
or iterate (example):
[for task in jsondecode(data.http.example.body).tasks: task.status]
Related
I am developing a telegram bot with python (telebot) , aws lambda and api gateway.
I have a problem in the lambda function and I can't understand why I have this kind of problem.
My lambda is this:
import telebot
import datetime
TOKEN = 'xxx'
def lambda_handler(event, context):
bot = telebot.TeleBot(TOKEN)
# Extract the message key over payload's body
message = json.loads(event['body'])
print(message)
# Split between three variables bellow
chat_id = message['chat']['id'] # Chat ID will guide your chatbot reply
sender = message['from']['first_name'] # Sender's first name, registered by user's telegram app
text = message['text'] # The message content
if text.lower().strip() == "/time":
current_time = datetime.strftime(datetime.now(), "%H:%M:%S")
bot.send_message(chat_id, "Right now its {} UTC.".format(current_time))
else:
pass
The error I get, running the test is this:
Response
{
"errorMessage": "'body'",
"errorType": "KeyError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n message = json.loads(event['body'])\n"
]
}
The given json file:
{
"update_id": 0000000,
"message": {
"message_id": 000000,
"from": {
"id": 00000000,
"is_bot": false,
"first_name": "myname",
"last_name": "mysurname",
"username": "sursurname",
"language_code": "it"
},
"chat": {
"id": 000000,
"first_name": "myname",
"last_name": "mysurname",
"username": "sursurname",
"type": "private"
},
"date": 1654697178,
"forward_from": {
"id": 00000000,
"is_bot": false,
"first_name": "mysurname",
"last_name": "mysurname",
"username": "sursurname",
"language_code": "it"
},
"forward_date": 0000000000,
"text": "ciao"
}
}
I cannot understand why it is not able to read the body in any way, maybe I am in the wrong library? Do you have any suggestions to help me with this?
event['body'] is almost definitely not the correct key to access data passed through an event. The event passes information in a nested dictionary and you'll need to figure out how to drill down to the correct key.
I solved it by doing this:
json_str = json.dumps(event)
resp = json.loads(json_str)
chat_id = resp['message']['chat']['id']
message_text = resp['message']['text']
message_chat_id = resp['message']['chat']['id']
message_username = resp['message']['from']['first_name']
bot = telebot.TeleBot(TOKEN)
bot.send_message(chat_id, "Hey, I understood this message!, hi {}".format(message_username))
I've created a JSON server as a temporary backend service to read and edit user data.
When I do a read request like this:
http://localhost:3001/users?playerId=1193212
The read request is successful and I get the following response:
[
{
"playerId": "1193212",
"partnerId": "506",
"email": "hhristova+qahriprmpaal16#gan.com",
"alias": "qahriprmpaal16",
"forename": "Christine",
"surname": "Bell",
"mobileNumber": "9792694595",
"testAccount": "testAccount",
"suspended": "accountSuspended"
}
]
However, when I do a put request like this:
PUT
http://localhost:3001/users?playerId=1193212
{
"playerId" : "1193212",
"suspended": "accountSuspended"
}
I get a 404 not found response.
How do I do a put request with JSON server?
I've also tried
http://localhost:3001/users/1193212
I'm sending accept and content-type headers = "application/json"
I'm trying to send several PUT request to a specific endpoint.
I am using a CSV file within the columns and values, the name of the different columns reference the different variables inside the body, do you get me?
This is the endpoint:
{{URL_API}}/products/{{sku}} --sku is the id of the product, i created that variable because i use it to pass the reference
This is the body that i use:
{
"price":"{{price}}",
"tax_percentage":"{{tax_percentage}}",
"store_code":"{{store_code}}",
"markup_top":"{{markup_top}}",
"status":"{{status}}",
"group_prices": [
{
"group":"{{class_a}}",
"price":"{{price_a}}",
"website":"{{website_a}}"
}
]
}
I don't want to use the body anymore.. sometimes some products have more tan 1 group of prices, i mean:
"group_prices": [
{
"group":"{{class_a}}",
"price":"{{price_a}}",
"website":"{{website_a}}"
},
{
"group":"{{class_b}}",
"price":"{{price_b}}",
"website":"{{website_b}}"
}
Is it possible to create something like this in the CSV file?
sku,requestBody
99RE345GT, {JSON Payload}
How should i declare the {JSON Payload}?
Can you help me?
EDIT:
This is the CSV file i used:
sku,price,tax_percentage,store_code,markup_top,status,class_a,price_a,website_a
95LB645R34ER,147000,US-21,B2BUSD,1.62,1,CLASS A,700038.79,B2BUSD
I want to pass the JSON within the CSV file, i mean
sku,requestBody
95LB645R34ER,{"price":"147000","tax_percentage":"US-21","store_code":"B2BUSD","markup_top":"1.62","status":"1","group_prices":
[{ "group":"CLASS A","price":"700038.79","website":"B2BUSD"}]}
Is it okay?Should i specify anything on the request body or not? I read the documentation posted in POSTMAN website but i did not find an example like this.
As you're using JSON data as a payload, I would use a JSON file rather than a CSV file in the Collection Runner. Use this as a template and save it as data.json, it's in the correct format accepted in the Runner.
[
{
"sku": "95LB645R34ER",
"payload": {
"price": "147000",
"tax_percentage": "US-21",
"store_code": "B2BUSD",
"markup_top": "1.62",
"status": "1",
"group_prices": [
{
"group": "CLASS A",
"price": "700038.79",
"website": "B2BUSD"
}
]
}
},
{
"sku": "MADEUPSKU",
"payload": {
"price": "99999",
"tax_percentage": "UK-99",
"store_code": "BLAH",
"markup_top": "9.99",
"status": "5",
"group_prices": [
{
"group": "CLASS B",
"price": "88888.79",
"website": "BLAH"
}
]
}
}
]
In the pre-request Script of the request, add this code. It's creating a new local variable from the data under the payload key in the data file. The data needs to be transformed into a string so it's using JSON.stringify() to do this.
pm.variables.set("JSONpayload", JSON.stringify(pm.iterationData.get('payload'), null, 2));
In the Request Body, add this:
{{JSONpayload}}
In the Collection Runner, select the Collection you would like to run and then select the data.json file that you previously created. Run the Collection.
I'm trying to change the JSON response I'm getting from a request I make using Logic apps like this:
This request will get the following response:
{
"invoiceID":1,
"formType":"invoice",
"amount":449,
"currency":"eur",
"description":"Invoice real estate",
"period":{"end":20122019,"start":20122020},
"owner":{
"id":91434,
"firstname":"John",
"lastname":"Doe",
"dateOfBirth":1121993,
"phoneNumber":345435435,
"countryOfBirth":"Nederland",
"IBAN":"NL28 ABNA 743734g763474324"
},
"property":{
"id":105,
"type":"apartment",
"address":"ghost lane 13",
"ZIP":"7888 CK",
"State\/Province":"Groningen",
"country":"Nederland",
"construction-year":15072009,
"previousOwners":9
},
"previousProperties":[54,193,11,454,18]
}
Now I'm trying to compose the JSON of above to another json structure, for example to this:
{
"general": {
"invoiceID": 12,
"formType": "invoice",
"amount": 449,
"currency": "eur",
"description": "Invoice real estate",
"period": {
"end": 20122019,
"start": 20122020
}
}
}
I tried using the Compose action for this:
Finally I return the response:
]]
This setup does not work and I'm getting the following error:
{"error":{"code":"NoResponse","message":"The server didbnt receive a answer of the upstream-server. The trace-id from the request is 08586376520125765844944852801CU36."}}
When I remove the Compose action from the logic app designer, the flow does work but I'm getting the original JSON response.
UPDATE
I'm getting the following options for my compose configuration:
From your pic, your Response body set the HTTP action body, you need set the Compose output, the below is my test result and i don't get your error. Maybe you could have a look. And looks like in your compose action, you set the value with wrong value, from your requirements, you need set them with HTTP get action output.
I test with a HTTP trigger.
And the below is my compose input:
"general": {
"amount": "#body('HTTP')['amount']",
"currency": "#body('HTTP')['currency']",
"description": "#body('HTTP')['description']",
"formType": "#body('HTTP')['formType']",
"invoiceID": "#body('HTTP')['invoiceID']",
"period": {
"end": "#body('HTTP')['period']['end']",
"start": "#body('HTTP')['period']['start']"
}
}
And here is the response what my postman get.
Hope this could help you.
I have a restAPI code from a programmer from JNE, company stands for delivery service.
They say that this API can be run in POSTMAN (Google Chrome Application)
It works fine in the POSTMAN, where in this application I just need to insert the request URL (which I have got from the JNE company) and two header of keys and values as follow;
KEY VALUE
----------------------------------------------
username mycompany
api key 4534645756864234523424
The method for this is POST and when I posted it, it gives me the results as how expected.
My problem now is, how can I run this code in my page, so that I don't need to run this in postman.
I am just this day going to learn JSON if anybody can help me out with this.
[UPDATE QUESTION 1]
{
"version":1,
"collections":
[
{
"id":"c8b12431-8586-cbdd-aef7-056ec177509a",
"name":"asdasdadasdasdasd",
"timestamp":1415593872130,
"requests":
[
{
"collectionId":"c8b12431-8586-cbdd-aef7-056ec177509a",
"id":"d1b2ed66-781d-d02e-c4eb-0416dd3e07a1",
"name":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jak",
"description":"",
"url":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jak",
"method":"POST",
"headers":"username: mycompany\napi_key:089a12ffb8cd5009bdfa4ba5bdb9ee26\n",
"data":
[
{
"key":"username",
"value":"mycompany",
"type":"text"
},
{
"key":"api_key",
"value":"dsfsdfsdfs98d98sdfsdf9898dsfs",
"type":"text"
}
],
"dataMode":"params",
"timestamp":0,
"responses":[],
"version":2
}
]
}
],
"environments":[],
"headerPresets":[],
"globals":[]
}
From the update question above; my first question is: ]
In what format I have to save this file: JSON? or WHAT?
Should I save this file in one file with my webpage? or Can I save it as external file?
From the code above, I get the result as follow:
{
"detail": [
{
"code": "CGK10000",
"label": "JAKARTA"
},
{
"code": "CGK10100",
"label": "JAKARTA BARAT"
},
{
"code": "CGK10300",
"label": "JAKARTA PUSAT"
},
{
"code": "CGK10200",
"label": "JAKARTA SELATAN"
},
{
"code": "CGK10500",
"label": "JAKARTA TIMUR"
},
{
"code": "CGK10400",
"label": "JAKARTA UTARA"
}
]
}
If you have a look to the "label" it is generated from the key of the last string in the: "name":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jak",
The result of the label from the last string of jak, is what I want to insert in a dropdown html tag, in where the user will choose that (the name of the location).
[Update with complete code]
POST /tracing/mycompany/origin/key/jak HTTP/1.1
Host: api.jne.co.id:8889
Content-Type: application/json
username: mycompany
api_key: 089a12ffb8cd5009bdfa4ba5bdb9ee26
{
"version":1,
"collections":
[
{
"id":"c8b12431-8586-cbdd-aef7-056ec177509a",
"name":"asdasdadasdasdasd",
"timestamp":1415593872130,
"requests":
[
{
"collectionId":"c8b12431-8586-cbdd-aef7-056ec177509a",
"id":"d1b2ed66-781d-d02e-c4eb-0416dd3e07a1",
"name":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jakarta",
"description":"",
"url":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jakarta",
"method":"POST",
"headers":"username: mycompany\napi_key:089a12ffb8cd5009bdfa4ba5bdb9ee26\n",
"data":
[
{
"key":"username",
"value":"mycompany",
"type":"text"
},
{
"key":"api_key",
"value":"089a12ffb8cd5009bdfa4ba5bdb9ee26",
"type":"text"
}
],
"dataMode":"params",
"timestamp":0,
"responses":[],
"version":2
}
]
}
],
"environments":[],
"headerPresets":[],
"globals":[]
}
I have saved this file as jne.json and jne.html but the browser just show the full code insted show the result as how the postman does. I think there are many things I am missing here.
The POST request would look something like the following
POST /tracing/mycompany/origin/key/jak HTTP/1.1
Host: api.jne.co.id:8889
Content-Type: application/json
username: mycompany
api_key: 089a12ffb8cd5009bdfa4ba5bdb9ee26
{
... your JSON ...
}
You can save JSON with the .json file extension. If your request is always the same you can save this file with your webpage, but normally an HTTP request is constructed before sending (that means you normally send different requests).
To fill the dropdown list you just have to parse the JSON response.