JSON Server PUT request not found - json

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"

Related

Terraform HTTP Request body manipulation

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]

How to use change JSON structure using Compose action in Azure Logic Apps?

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.

How to check Post request of this format using Postman

I am checking json object post request using postman.The data is in format
{
"user": {
"first_name": "Satyam",
"last_name": "Gondhale",
"username": "satyam#gmail.com",
"email": "satyam#gmail.com",
"groups": [],
"is_active": true
},
"phone": "9028571487",
"address": "Pune"
}
Every time I am sending request I am getting error in postman is
{
"user": [
"This field is required."
]
}
How to send this fields as a Post request using Postman to Django server
Start Postman
Click on New.
Select New Request
Change the Method from Get to Post
Set the URL you are using in the rightside of the field POST.
Then navigate to the Tab Body and paste your Code:
{
"user": {
"first_name": "Satyam",
"last_name": "Gondhale",
"username": "satyam#gmail.com",
"email": "satyam#gmail.com",
"groups": [],
"is_active": true
},
"phone": "9028571487",
"address": "Pune"
}
See picture for example:
I think you also need to add a Header.
Navigate to the tab Headers
in the field Key add Content-type
in the field Value add application/json
Click then on Send.
then you should get a response.
Voilaa!
Regards
Xsi

Posting data in a JSON file in AngularJS - (using django REST here to create that JSON file.)

I am using AngularJS along with Python & Django and Django REST API.
There is a JSON file created by the REST API and I need to post data into it using Angular $http.post().
I need to know if it is possible or not.
Im majorly getting 403(Forbidden) and 400(BAD REQUEST) errors on post..
$http({
method: 'POST',
url: '<JSON FILE URL>',
data: $scope.tmpDataSet,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}});
This is my .post() method. where im fetching the data from a form made in angular and storing it in 'tmpDataSet'. Its being created properly and im able to store into the array.
Im just not able to write it into the JSON file.
The structure of my JSON file is
{
"count": 6,
"next": null,
"previous": null,
"results": [
{
"name": "fff",
"mobile_no": "fff",
"email": "n#gmail.com",
"message": "dfdf",
"id": 1
},
{
"name": "asd",
"mobile_no": "0987654321",
"email": "asd#gmail.com",
"message": "no",
"id": 2
}
]
If any more code detail is needed please comment.
This issue was solved by adding CSRF tokens to the angular app and using the regular $http.post() function with all the parameters.
app.config(function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
});
and,
$http.post('<URL>', item).error(function(data,status,headers,config){
console.log('COULDNT POST!');
}).success(function(data, status, headers, config){
console.log('POSTED!');
});

Generate POSTMAN in webpage with JSON or something

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.