Retrofit Convert Post to Get request - retrofit

i have post request which i want to convert to get request any way i can do . as of now i know is i have to change all stuff like
#POST("apiname")
Call<ResponseBody> getBasic(#Body DataRequest data);
and DataRequest is having say 5 params . now
#GET("apiname")
Call<ResponseBody> getBasic(
#Query("one") String one,
#Query("two") String two,
#Query("three") String three)..;
so in this way i have to add number of variables to #Query but problem is if its 10 then i have to add 10 time . is any other workaround where i can convert POST to GET
any way i can pass POJO model and it converted to get request format

You can use #QueryMap to pass multiple query params dynamically. So in your case:
#GET("apiname")
Call<ResponseBody> getBasic(#QueryMap Map<String, String> options);
You can then define a method in your pojo to convert it to a Map<String, String>

Related

Optional Query Params and composed data

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

Url.OriginalString vs Url.AbsoluteUri

What is the difference between HttpContext.Current.Request.Url.OriginalString and HttpContext.Current.Request.Url.AbsoluteUri?
I need to get the URL in the page load, but confused about the both.
Depending on the url, you could get the same result. It depends how exact you want it to be, if you want all fragments, and how you want to use it.
The following is for Uri but I think it applies to Url too (a url is a uri, essentially).
AbsoluteUri
The AbsoluteUri property includes the entire URI stored in the Uri instance, including all fragments and query strings.
This
Uri baseUri= new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today");
Console.WriteLine(myUri.AbsoluteUri);
will return
http://www.contoso.com/catalog/shownew.htm?date=today
https://msdn.microsoft.com/en-us/library/system.uri.absoluteuri(v=vs.110).aspx
Uri.OriginalString
Gets the original URI string that was passed to the Uri constructor.
The following example creates a new Uri instance from a string. It illustrates the difference between the value returned from OriginalString, which returns the string that was passed to the constructor, and from a call to ToString, which returns the canonical form of the string.
// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.ConToso.com:80//thick%20and%20thin.htm");
// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version. OriginalString gives the orginal
// string that was passed to the constructor.
// The following outputs "http://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString());
// The following outputs "HTTP://www.ConToso.com:80//thick%20and%20thin.htm".
Console.WriteLine(uriAddress.OriginalString);
https://msdn.microsoft.com/en-us/library/system.uri.originalstring(v=vs.110).aspx

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.

asp.net return data in Page_Load

If a query string param exists in my page request I want to query the database on the server in the Page_Load and then return the result to the client. I can do the query string param check and query the DB but how do I return the data to the page and on the javascript side how do I access that data?
Ideally I would return JSON of an object structure and it would be returning an array of them.
Yes, returning JSON would be the best option. I'm not sure how you query your database (Do you use LINQ or ADO.NET DataTables, etc)
If you don't have custom object of type you want to send, I recommend you create one. Then you should get an array of them.
Example:
public class Person {
string Name { get; set; }
int Age { get; set; }
}
Person[] pArr = new Person[5];
Then you can use a third party library like this to create an string representaion of that array in JSON.
string json = JsonConvert.SerializeObject(product);
Then you write that json string to the Response object so its sent down to the client, by overriding the Render method of the page.
// Don't expect this code to work as it is, but take this as a guidance
Response.Clear();
Response.Write(json);
Response.Close();
on the client side use jQuery library send a request to page, and process the JSON response for you.
$.getJSON('url', function(data) {
//process data
});
Here is my suggestion if you don't want to use an AJAX request for this:
Use the objects as you would normally do in the page_load, and convert it to a JSON string as explained above.
Then use ClientScriptManager to create an JavaScript variable on the client side when it loaded.
ClientScript.RegisterClientScriptBlock(typeof(Page), "unique_key", "var myObjectList = " + json, true);
After this when the page loads you will have an variable named "myObjectList" with the list of objects without having to make a different AJAX call.
You can directly refer that variable in your javascript and do necessary processing.
Hope this helps.

Resources