How to search for text inside decoded json data? - flutter

I want to search for particular texts inside the data received from an API call using the response.body function with the http library in Flutter. I have decoded the data with jsonDecode(data) into a var called decodedData.
How can i search for particular field inside this decodedData?

It's a map and you can use containsKey or containsValue to search for a particular text

Well if you just want to search for a particular string from whole body .then you can follow the steps Bellow
Convert the response body to a string using .tostring() method.
str1.contains('rem')
Use the .contains method to search for the dedired string

Related

How to parse json list into a string - Tweak the code so that I can display a single Json, just as I can display a List of Json

I am getting this error:
A value of type 'UserModel' can't be assigned to a variable of type 'List<UserModel>'.
I am using Postman to generate my api service code (http request that get api from internet). And I am using https://app.quicktype.io/ to generate my User Model class.
I saw a flutter tutorial which parsed json into a List because the json was actually a List itself. But In this case, Json is a single item (see pictures for difference). How do i Parse it? and display in a ListView.builder widget?
You can re create this problem using this api link: https://reqres.in/api/users?page=1
My code snippet to parse json into a List is:
if (response.statusCode == 200) {
String tempStoreDataFromApi = await response.stream.bytesToString();
List<DataModel> dataList_1 = dataModelFromJson(tempStoreDataFromApi);
Samples for how a List of json looks, vs how a single item of json looks.
Your api response is a Map. You should use ["data"] on it if you want to access the list inside it, like this:
List<UserModel> userModelFromJson(String str)-> List<UserModel>.from(((json.decode(str)["data"]) as List).map((x)-> UserModel.fromJson(x)));

Access nested body property from HTTP resolver(AppSync)

I'm new to AWS AppSync and I am trying to access certain body property(from HTTP response) in my resolver's response mapping template.
For example: I am able to present the response as is via $util.toJson($ctx.result.body), but when I try to get some of the nested body properties it fails.
For example, imagine the body looks like this:
{
about:{
"firstName":"Chuck",
"lastName":"Norris"
}
}
and $util.toJson($ctx.result.body.about) returns null. Any thoughts?
I found a way extract the parsed body in the following way:
#set ($parsed_body = $util.parseJson($ctx.result.body))
And then I am able to access the properties via dot notation:
parsed_body.about.firstName
The part I was missing is $util.parseJson(<json-string>)
It seems that the body is a JSON string.

How to pass a map as query parameter

I'm using labstack Echo (v4.1.11) go framework to create a REST API.
How do we/can we pass map query param in the URL? I tried adding the following struct for the request body
PaginationRequest struct {
Page int64 `query:"page"`
Limit int64 `query:"limit"`
Filter map[string]string `query:"filter"`
}
and hoping I could got this url http://host/endpoint?page=1&limit=10&filter[status]=1&filter[user_id]=12 to work (read the filter["status"] and filter["user_id"]
But when I tried binding the incoming request to PaginationRequest it only read the page and limit parameter and filter = nil
I tried debug it into the echo/bind.go the Bind function in reading the QueryParam actually read filter[status] as plain string "filter[status]" as key.
Is there anyway we can pass the map to the url query param? or we really have to be specific in here?

What is the proper way to add a map in a REST request

I'm using Google Endpoint and for one of my entities I want to create a POST request that adds a map of properties. What is the right way to do it?
I know Google Endpoint can receive a Collection as a parameter, but I want to add a map (unknown key values).
Should I pass a JSON as a parameter or just add the JSON in the body of the request and extract it from the HttpServletRequest object?
I would avoid passing it as a parameter. You can send it in the body of the request and then use json library to get a python object.
https://docs.python.org/3/library/json.html
Every JSON object is a map, so it looks like the most obvious choice. GSON makes it easy, but you can use other parsers too.
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson("{'k1':'apple','k2':'orange'}", type);

HTML form POST method with querystring in action URL

Lets say I have a form with method=POST on my page.
Now this form has some basic form elements like textbox, checkbox, etc
It has action URL as http://example.com/someAction.do?param=value
I do understand that this is actually a contradictory thing to do, but my question is will it work in practice.
So my questions are;
Since the form method is POST and I have a querystring as well in my URL (?param=value)
Will it work correctly? i.e. will I be able to retrieve param=value on my receiving page (someAction.do)
Lets say I use Java/JSP to access the values on server side. So what is the way to get the values on server side ? Is the syntax same to access value of param=value as well as for the form elements like textbox/radio button/checkbox, etc ?
1) YES, you will have access to POST and GET variables since your request will contain both. So you can use $_GET["param_name"] and $_POST["param_name"] accordingly.
2) Using JSP you can use the following code for both:
<%= request.getParameter("param_name") %>
If you're using EL (JSP Expression Language), you can also get them in the following way:
${param.param_name}
EDIT: if the param_name is present in both the request QueryString and POST data, both of them will be returned as an array of values, the first one being the QueryString.
In such scenarios, getParameter("param_name) would return the first one of them (as explained here), however both of them can be read using the getParameterValues("param_name") method in the following way:
String[] values = request.getParameterValues("param_name");
For further info, read here.
Yes. You can retrieve these parameters in your action class.
Just you have to make property of same name (param in your case) with there getters and setters.
Sample Code
private String param;
{... getters and setters ...}
when you will do this, the parameters value (passed via URL) will get saved into the getters of that particular property. and through this, you can do whatever you want with that value.
The POST method just hide the submitted form data from the user. He/she can't see what data has been sent to the server, unless a special tool is used.
The GET method allows anybody to see what data it has. You can easily see the data from the URL (ex. By seeing the key-value pairs in the query string).
In other words it is up to you to show the (maybe unimportant) data to the user by using query string in the form action. For example in a data table filter. To keep the current pagination state, you can use domain.com/path.do?page=3 as an action. And you can hide the other data within the form components, like input, textarea, etc.
Both methods can be catched in the server with the same way. For example in Java, by using request.getParameter("page").