1. Ramón San Félix Ramón
  2. SnapDevelop
  3. Friday, 31 July 2020 09:07 AM UTC
Hi.
What is the most elegant way of sending many parameters to WebApi to do Datawindow's retrieve ?

In PowerBuilder I have a datawindow like this::

String ls_company
Integer li_year
Datetime ldt_dateFrom
Datetime ldt_dateTo
String ls_CustomerFrom
String ls_CuastomerTo

ls_company ="1"
li_year=2020
ldt_dateFrom = Datetime("01-01-20")
ldt_dateTo = Datetime("31-12-20")
ls_CustomerFrom = "1"
ls_CuastomerTo = "99999"

dw_1.Retrieve(ls_
company, li_year, ldt_dateFrom, ldt_dateTo, ls_customerFrom, ls_customerTo)

Can you give me an example of the controller in C# and the implementation in PowerBuilder?

Something like this in PowerBuilder?

RestClient lrc_RestClient
String ls_ResPonse, ls_url

lrc_RestClient = Create RestClient

lrc_RestClient.SetRequestHeaders("Content-Type:application/json;charset=UTF-8~r~nAccept-Encoding:gzip")
ls_url = "http://localhost:5000/api/D_customers/retrieve/"+ls_company+"/"+string(li_year)+"/"+string(ldt_dateFrom)+"/"+ string(ldt_dateTo)+"/"+ ls_customerFrom+"/"+ ls_customerTo
lrc_RestClient.SendGetRequest(ls_url, ls_ResPonse)

And
Something like in SnapDevelop?
// Controller
 //GET api/D_Customers/Retrieve/
{company}/{year}/{dateFrom}/{dateTo}/{customerFrom}/{customerTo}
         [HttpGet("{company}/{year}/{dateFrom}/{dateTo}/{customerFrom}/{customerTo}")]
        [ProducesResponseType(typeof(IDataStore<D_Customers>), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]
        public ActionResult<IDataStore<D_Customers>> Retrieve(string ls_company, integer li_year, datetime ldt_dateFrom, datetime ldt_dateTo, string ls_customerFrom, string ls_customerTo)
        {
            try
            {
                var result = _id_Customersservice.Retrieve(company, year, dateFrom, dateTo, customerFrom, customerTo);

                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }
        }
How to use parameters with dates correctly?

Best regards.



Ramón San Félix Ramón Accepted Answer Pending Moderation
  1. Thursday, 6 August 2020 08:54 AM UTC
  2. SnapDevelop
  3. # 1

OK, 

// Something like this in PowerBuilder?

RestClient lrc_RestClient
String ls_ResPonse, ls_url, ls_json
JsonGenerator lnv_JsonGenerator
Long ll_RootObject

lrc_RestClient = Create RestClient

lrc_RestClient.SetRequestHeaders("Content-Type:application/json;charset=UTF-8~r~nAccept-Encoding:gzip")
ls_url = "http://localhost:5000/api/D_customers/retrieve"

lnv_JsonGenerator = Create JsonGenerator

ll_RootObject = lnv_JsonGenerator.CreateJsonObject()

lnv_JsonGenerator.AddItemString(ll_RootObject, "company", ls_company)
lnv_JsonGenerator.AddItemNumber(ll_RootObject, "year", li_year)
lnv_JsonGenerator.AddItemDateTime(ll_RootObject, "dateFrom", ldt_datefrom)
lnv_JsonGenerator.AddItemDateTime(ll_RootObject, "dateTo", ldt_dateto)
lnv_JsonGenerator.AddItemString(ll_RootObject, "customerFrom", ls_customerfrom)
lnv_JsonGenerator.AddItemString(ll_RootObject, "customerTo", ls_customerto)

ls_Json= lnv_JsonGenerator.GetJsonString()

//Send Post Request With Json Arguments to the Server
lrc_RestClient.SendPostRequest(ls_url, ls_json, ls_ResPonse)

dwc.ImportJson(ls_response)

// And Something like in SnapDevelop?

// Controller
[HttpPost]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<IDataStore> Retrieve(IDataUnpacker unpacker)
{
var company = unpacker.GetValue<string>("company");
var year = unpacker.GetValue<int>("year");
var dateFrom = unpacker.GetValue<DateTime>("dateFrom");
var dateTo = unpacker.GetValue<DateTime>("dateTo");
var customerFrom = unpacker.GetValue<string>("customerFrom");
var customerTo = unpacker.GetValue<string>("customerTo");

var packer = new DataPacker();

try
{
var result = _id_Customersservice.Retrieve(company, year, dateFrom, dateTo, customerFrom, customerTo);

return Ok(result);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}

}

Comment
There are no comments made yet.
Govinda Lopez @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 4 August 2020 17:19 PM UTC
  2. SnapDevelop
  3. # 2

Hi Ramón,

 

This type of implementation you are looking into https://localhost:5000/api/v1/Customers/Retrieve/{parm1}/{parm2}/{parm3}/{parmN} is used mostly if you want to create overloading of the same method. Then it would make sense to use this type of implementation. But if what you want is to actually keep it simple and tight, then you could also send a JSON with the parameters in it. That would be another way of doing it without ending up with a very long and complex URI structure.

 

Food for thoughts.

 

 

Regards,

Comment
There are no comments made yet.
Ramón San Félix Ramón Accepted Answer Pending Moderation
  1. Monday, 3 August 2020 11:22 AM UTC
  2. SnapDevelop
  3. # 3

I've also tried to pass arguments like this ...

Something like this in PowerBuilder?

RestClient lrc_RestClient
String ls_ResPonse, ls_url

lrc_RestClient = Create RestClient

lrc_RestClient.SetRequestHeaders("Content-Type:application/json;charset=UTF-8~r~nAccept-Encoding:gzip")
ls_url = "http://localhost:5000/api/D_customers/retrieve?company="+ls_company+"&year="+string(li_year)+"&datefrom="+string(ldt_dateFrom)+"&dateto="+ string(ldt_dateTo)+"&customerfrom="+ ls_customerFrom+"&customerto"+ ls_customerTo
lrc_RestClient.SendGetRequest(ls_url, ls_ResPonse)

And Something like in SnapDevelop?
// Controller
//GET api/D_Customers/Retrieve
[HttpGet]
[ProducesResponseType(typeof(IDataStore<D_Customers>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<IDataStore<D_Customers>> Retrieve(string ls_company, integer li_year, datetime ldt_dateFrom, datetime ldt_dateTo, string ls_customerFrom, string ls_customerTo)
{
try
{
var result = _id_Customersservice.Retrieve(company, year, dateFrom, dateTo, customerFrom, customerTo);

return Ok(result);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}

Comment
  1. Logan Liu @Appeon
  2. Tuesday, 4 August 2020 16:42 PM UTC
Hi Ramon,

I want to add:

If there is any sensitive data or these parameters are very complex, a better solution is to call the HTTP POST method to send the parameters in the request body.

You can use the JSONPackage object to combine parameters and get the JSON string on the PB client, and then POST it to your WEB API. You can use the IDataUnpacker type parameter in the WEB API to receive them.

Regards,

Logan
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.