PUT Method payload for REST Webservice - javascript

I have object as
{
"sch": {
"name":"ABC",
"time_zone":"Eastern Time (US & Canada)",
....
}
}
I am trying to create payload for PUT method of REST web-service using javascript. I have created payload as:
var body={"sch.name":"CAD"};
but i am not able to update the name with new value CAD .
Am i doing this correctly for the "name" element?

Related

GitHub GraphQL API query for response simplification [duplicate]

Let's say I've got a GraphQL query that looks like this:
query {
Todo {
label
is_completed
id
}
}
But the client that consumes the data from this query needs a data structure that's a bit different- e.g. a TypeScript interface like:
interface Todo {
title: string // "title" is just a different name for "label"
data: {
is_completed: boolean
id: number
}
}
It's easy enough to just use an alias to return label as title. But is there any way to make it return both is_completed and id under an alias called data?
There is no way to do that. Either change the schema to reflect the client's needs or transform the response after it is fetched on the client side.

How i send a object from Java like a Javascript object?

I have an application Angular 7 frontend, Android mobile and a Laravel backend. I send an object in Angular 7 that is working fine on Laravel this is the object
{
"mov_entry_id": 72507,
"limitHour": "02/07/2019 13:40:27",
"plate": null,
"mov_vehicle_id": 1,
"mov_vehicle_name": "Carro",
"value": "8.00",
"mov_area_id": 7,
"mov_area_name": "AB",
"mov_user_id": 1,
"validated": "N",
"updated_at": "02/07/2019 13:20:27",
"mov_entry_ean13": "7800000725070",
"created_at": "02/07/2019 13:20:27"
}
I'm sending a post method from angular to laravel this is the backend part that receive this object
$entryObj = $request->input('entry');
$userid = $request->input('userid');
$hourValue = 0;
$validatedDate;
$response["success"] = 1;
$response["message"] = "Entrada validada com blabla";
$response['entryObj'] = $entryObj;
$response['mov_entry_ean13'] = $entryObj["mov_entry_ean13"];
return $response;
I get the object in "$request->input('entry');" in this part and I want to access the object like this $entryObj["mov_entry_ean13"];, when I send in Angular 7 with the Json as I show to you guys, my Laravel application access this is object fine, but my problem is on Android.
When I use Java in Android, I tried to make the object looks like the javascript way but with no success, I try JsonObject, JsonElement, and Map, this is my Retrofit API:
#FormUrlEncoded
#POST("/api/validateentrymobi")
public Call<JsonObject> validateTicket(
#Field("entry") JsonObject entry,
#Field("userid") String user_id
);
When I use I'm getting this error "Illegal string offset "mov_entry_ean13"", like is something wrong with my object that I'm sending, this is the object from java
{"mov_entry_id":72507,"plate":null,"mov_vehicle_id":1,"mov_vehicle_name":"Carro","value":"8.00","mov_area_id":7,"mov_area_name":"AB","mov_user_id":1,"validated":"N","mov_entry_ean13":"7800000725070","created_at":"02/07/2019 13:20:27","updated_at":"02/07/2019 13:20:27","validated_date":"2019-07-02 13:40:27"}
What I'm doing wrong?
You need to create a POJO and set up a json converter for Retrofit to use.
Setup Retrofit
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create()) // this requires implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
.build()
Create a POJO class annotated with Gson
// this will act as the top level json object {}
class MyBody {
#SerializedName("mov_entry_id")
int movEntryId;
// write your fields here
}
Use the POJO as the body for Retrofit request
// you don't need the form unless your backend requires it
#POST("/api/validateentrymobi")
public Call<ResponseBody> validateTicket( // #NOTE the return type, you can create a POJO class similar to how I created MyBody and replace ResponseBody with it. But ResponseBody should work.
#Body MyBody myBody
);

How to retrieve data from firebase using the key value

I am working on a web application and I purely used javascript to store read and delete data. So it's a blog-type application in which I currently am working on, and my database schema is like the following picture.
Now the problem is I want to retrieve values of posts (like description, imageurl, task) by key value postCategory which is "software engineering".
You need to use the equalTo
postsRef.orderByChild("postCategory").equalTo("software engineering").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
System.out.println(dataSnapshot.getKey());
}
// ...
});

API Connect - 500 error when including basic Javascript

I'm trying some basic API Connect tutorials on IBM's platform (running locally using loopback) and have got completely stuck at an early point.
I've built a basic API service with some in-memory data and setter / getter functions. I've then built a separate API which takes two GET parameters and uses one of my getter functions to perform a search based on two criteria. When I run it, I successfully get a response with the following JSON object:
[{"itemId":1,"charge":9,"itemSize":2,"id":2}]
I've then tried to add a piece of server logic that modifies the response data - at this point, I'm just trying to add an extra field. I've added a Javascript component in the Assemble view and included the following code (taken from a tutorial), which I thought should modify the message body returned by the API while still passing it through:
//APIC: get the payload
var json = apim.getvariable('message.body');
//console.error("json %s", JSON.stringify(json));
//same: code to inject new attribute
json.platform = 'Powered by IBM API Connect';
//APIC: set the payload
//message.body = json;
apim.setvariable('message.body', json);
Instead of getting an extra JSON parameter ("platform"), all I get is a 500 error when I call the service. I'm guessing that I'm doing something fundamentally wrong, but all the docs suggest these are the right variable names to use.
You can't access json.platform but at that point json variable is json type. Are you sure that you can add a property to a json type variable if your json object lacks of that property? I mean: What if you first parse the json variable of json type to a normal object, then add new property, and finally stringify to json type again for body assigning purposes?
var json = JSON.parse(apim.getvariable('message.body')); //convert to normal object
json.platform = 'Powered by IBM API Connect'; //add new property
apim.setvariable('message.body', JSON.stringify(json)); //convert to json again before setting as body value
You need to get the context in some determined format, and in this function do your logic. For example if your message is in json you need to do:
apim.readInputAsJSON(function (error, json) {
if (error)
{
// handle error
apim.error('MyError', 500, 'Internal Error', 'Some error message');
}
else
{
//APIC: get the payload
var json = apim.getvariable('message.body');
//console.error("json %s", JSON.stringify(json));
if(json){
//same: code to inject new attribute
json.platform = 'Powered by IBM API Connect';
//APIC: set the payload
//message.body = json;
apim.setvariable('message.body', json);
}
}
});
Reference:
IBM Reference
You have the message.body empty, put a invoke/proxy policy before your gateway/javascript policy for example.

x-editable submit additional "array" to the total AJax POST data

I'm trying to add an array of object to the post data of a group of bootstrap x-editable in my jsp page.
I recover the data from a table, and build an array of object. After do that, I need to append this array to the list of the other value posted by the editables in the page.
I've an html table with id="user" and this code recover data from it and build the array of object. This is my code, that works and produces the correct object:
function recover_data() {
console.log("Starting recover");
var beneficiary_list = [];
$('.lav').each(function() {
var obj = {'company': $(this).attr('id'), 'num':$(this).text() };
beneficiary_list.push(obj)
});
console.log(beneficiary_list); //output to check array produced
return beneficiary_list;
};
this function is what I call from a save button, that launch my global submit retrieving data from all editable in the page and adding in the "params" the array returned from recover_data(), in this way:
jQuery('#data a').editable('submit',{
url:'test/prove/data_received',
params: function(params) {
params.beneficiary = recover_data();
return params;
},
success:function(resp){ ... }
})
but testing with Postman (Chrome) the produced output to POST, is without the array. Are submitted only the "editables" data, but no "beneficiary" field with array of that values adding. The missing field must be like
"beneficiary": [0: company: "1", num: "22" ..etc..]
How can I "append" to the produced POST data from a group of editables an array of objects, like that I've show?
Looking to this page is possible
Question on github x-editable page
I don't understand why don't works...I follow the example written on second post from vitalets, he wrote:
editable({
...
params: function(params) {
params.checked = ... //get array of checked values
return params;
}
});
To Explain better what I need: I've a Java Controller (servlet) that receive a modelView object (POJO) with several fields (that came from other editables in the page) and a field "ArrayList beneficiary" where Beneficiary is a small object of two fields "company" and "num"
public Class Beneficiary{
private String company;
private String num;
//then get e set methods..
}
instead, my POJO is like:
import java.util.ArrayList;
import Beneficiary;
public class Module {
private Integer id;
private String titolo;
private String companyRating;
private ArrayList<Beneficiary> beneficiary;
//get e set methods
In the console I obtain the correct object for that voice
but not in the post object that is like
instead I need that the produced output "beneficiaries" field to POST url is something similar to this example (found on internet)
How can I obtain a list of that object from my JSP page when submit?
Someone can help me?
params is an option for editable object, while you are using it as options for ajax call.
Your code should look like:
$('#data a').editable({
url: "/post"
});
and then the submit() (note selector for all of your editable objects):
$('.editable').editable('submit', {
data: {beneficiary: recover_data()},
success:function(resp){ ... }
});
Working fiddle. Check your console to see logs

Categories

Resources