1. Simone Olianti
  2. SnapObjects
  3. Friday, 20 September 2019 16:01 PM UTC

Hello, i am quite new to web apis but following the "CRUD Operations with .NET DataStore" i was able to publish my first web api, created my own data context and successfully generated my first c# datastore model. Also created the controller to retrieve data and successfully accessed it from a pb application using RESTClient.
How cool is that? :)

Now my question is:
Having a datastore with a retrieval argument (i.e. date type) how do i call the method to retrieve data passing a parameter? What should i put in the URL?

this is what i did in the services interface
using PowerBuilder.Data;
namespace StartWebApi1.Services
{
    public interface IDayFatService
    {
        IDataStore RetrieveByDate(string dataoggi);
    }
}

and the service like this:

        public IDataStore RetrieveByDate(string dataoggi)
        {
            var dss_DayFat = new DataStore("dss_dayfat", _context);
            dss_DayFat.Retrieve(dataoggi);
            return dss_DayFat;
        }

in the controller:

        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]
        public ActionResult RetrieveByDate(string dataoggi)
        {
            IDataStore dss_DayFat = new DataStore("dss_dayfat");
            try
            {
                dss_DayFat = _dayfatservice.RetrieveByDate(dataoggi);
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }
            if (dss_DayFat.RowCount <= 0)
            {
                return NotFound("No rows retrieved.");
            }
            else
            {
                return Ok(dss_DayFat);
            }
        }  

first of all i am not sure if i have to use a "string" type to define the parameter, but how do i build the url to call the api from pb?

inv_RestClient.sendgetrequest("http://xxx.xxx.xxx.xxx:82/api/dayfat/retrievebydate???????", ls_response)

any help would be appreciated

TIA, 

  simone

 

Accepted Answer
Govinda Lopez Accepted Answer Pending Moderation
  1. Friday, 20 September 2019 18:45 PM UTC
  2. SnapObjects
  3. # Permalink

Hi Simone,

 

You can do that by adding the argument to your URL. Something like this: https://xxx.xxx.xxx.xxx:82/api/dayfat/retrievebydate?dataoggi=‘18/10/1983’

 

You can also accept a datetime variable. And, you can also add the variables as part of your URL. In example: https://xxx.xxx.xxx.xxx:82/api/dayfat/retrievebyid/101

 

In order to do that, you need to decorate your controller method. You do that by adding a token into the decorated HttpGet Attribute like so: [HttpGet(“{Id}”)]

 

I hope this helps!

 

Regards,

Comment
  1. Simone Olianti
  2. Friday, 20 September 2019 23:15 PM UTC
Thank you Govinda! will try it asap
  1. Helpful
There are no comments made yet.


There are replies in this question but you are not allowed to view the replies from this question.