Optional Query Params and composed data - http

I'm trying to figure it out how to build a query string with some important information to be fetched. For example, I have this struct I'm going to decode the query into:
SomeObject{
Names []string
Attr Attribute
}
Atribute{
Group string
City string
}
So, initially I understand passing the names in the query string should look like this:
HTTP://localhost:8000/api/retrieve?names=oneName&names=anotherName
And I can get the names[oneName anotherName] correctly. But now I must build the query to get the attributes in order to get: attributes{group{someGroup} city{Delhi}}. How should the query string be written????
Question it's not related to the HTTP request handlers, but how the params should look to get correctly
Thanks in advance

Related

Why does update_post_meta change the value of my meta key?

When I use the update_post_meta() function to change a value of a key:
when it's a normal string it works
but when the string is the same as a JSON file it appends some extra string.
For example, when I save this string:
a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}
It is saved on the wp_postmeta table as:
s:162;"a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}"
When I use a short string there are no problems. How can solve this?
My code:
$edd_files='a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}';
update_post_meta($download_id,'edd_download_files',$edd_files);
The reason why this is happening is because update_post_meta() serializes the value you pass as third parameter (see update_metadata()).
Your $edd_files variable is a serialized array -not a "JSON file"- and update_post_meta() is serializing it again before saving it to the database, hence the reason why your serialized string changed like that.
I don't know why you're assigning $edd_files a serialized string but you can convert it back to an array using the maybe_unserialize() function before saving it as a post meta and then the value will be saved on the database as a serialized string as expected:
$edd_files = 'a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}';
// Convert serialized string back into an array
$edd_files = maybe_unserialize($edd_files);
update_post_meta($download_id, 'edd_download_files', $edd_files);
Result:

how to pass multiple key/value pairs in a single variable using query string?

I have one requirement like passing multiple values in the query string in a single variable.
Id=(refine_1=cgid=womens&refine_2=c_refinementColor=Black&refine_3=price=(0..500))
Is it possible to accept value like above sample from the query string?if yes,please tel me how to achieve this?
You should URL encode it:
?id=(refine_1%3Dcgid%3Dwomens%26refine_2%3Dc_refinementColor%3DBlack%26refine_3%3Dprice%3D(0..500))
Now assuming that your controller action takes an id parameter:
public ActionResult SomeAction(string id)
{
...
}
the value of this parameter inside the action will be (refine_1=cgid=womens&refine_2=c_refinementColor=Black&refine_3=price=(0..500)).
You could bring this even a step further and write a custom model binder that will parse this value and bind it to a view model containing those properties that your controller action could take as parameter instead of a id string parameter.

asp.net WebAPI list in Get request

I understand in Web API Get request, we can pass the list of inputs in query string like
Products?id=1&id=2&id=3
so the Get action method in ProductsController will be
Get([FromURI] int[] id)
{
}
My question now is, if the list of inputs to the Get call is much more (assume 1000), what is the recommended approach?
Comma separated values in query string will be some help. But that won't help in my case.Is there any other way to pass these inputs other than in query string?

multiple dispatcher is requiered

did we need multiple request-dispatcher to send multiple value to the same page
I have written this.
String name=rs.getString("itemname");
String code=rs.getString("itemcode");
String lpr=rs.getString("lastpurchase");
String ur=rs.getString("unitrate");
String pq=rs.getString("pquantity");
String cpq=rs.getString("costpquan");
ServletContext context= getServletContext();
RequestDispatcher rd=context.getRequestDispatcher("/index.jsp")
rd.forward(request,response);
I need to send all these variables to a same page.
No, you don't need multiple dispatchers. You simply need to store each value in a separate request attribute. A better option would be to create an object (Item, for example) containing all these values, and to store this object in a single request attribute.
Item item = new Item(name, code, lpr, ur, pq, cpq);
request.setAttribute("item", item);
rd.forward(request,response);
You should also use much better names for your variables.

Magic Casting to collection of results

In the Simple.Data examples, there is an example of 'Magic Casting':
// When you assign the dynamic record to a static type, any matching properties are auto-mapped.
var db = Database.Open();
Customer customer = db.Customers.FindByCustomerId(1);
Does Simple.Data also magically cast if there are multiple records returned? Something like this:
var db = Database.Open();
IEnumerable<Customer> customers = db.Customers.FindBySurname("Smith");
Obviously I have tried the above and it doesn't work ("Cannot implicitly convert type" from SimpleQuery to my concrete type). Any advice would be welcome.
FindBySurname returns a single record. If you use FindAllBySurname you'll get an enumerable, which should magic cast OK. (If for some reason it doesn't, you can call .Cast() on it.)

Resources