locked
How to make a GET RESTful Request RRS feed

  • Question

  • User-1664485818 posted

    Hi folks, looking for some advice on my code, basically is this how you make a Get RESTful request, I also need to pass 2 parameters within the urs!

        private string RequestData()
            { 
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.example.com?param1=good&param2=bad");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "GET";
    
                    //Get Response
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        return result;
                    }
            }

     

    Tuesday, July 5, 2016 9:36 AM

Answers

All replies

  • User36583972 posted

    Hi brucey,

    Your RequestData methods seem ok, you can define your controller method like the below.

          // GET api/values?catalognbr=0.2&campusstr=name
            [AllowAnonymous]
            public HttpResponseMessage GetBiosample(float catalognbr, string campusstr)
            {
                //---------catalognbr, campusstr
                HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
                response.Content = new ObjectContent<ObjectItem>(Item, new XmlMediaTypeFormatter());
    
                return response;
            }

    You can debug your program.

    Best Regards,

    Yohann Lu

    Tuesday, July 5, 2016 9:58 AM
  • User-1664485818 posted

    Hi folks, I need to include curl -H 'Context-type:application/json' within the url not really sure how to do this, server responce so far 404, any help much appreciated,

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://www.check-mot.service.gov.uk/api/v1/mot-history/{0}/{1}/", registration, make));
    httpWebRequest.ContentType = "application/json";                      
    httpWebRequest.Method = "GET";

    Tuesday, July 5, 2016 8:19 PM
  • User36583972 posted

    Hi brucey,

    From your error message, I think you have some wrong in Routing.

    Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

    You can refer the following tutorial and modify your code.

    Attribute Routing in ASP.NET Web API 2:

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    Web API Route and Route Prefix:

    http://www.c-sharpcorner.com/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 6, 2016 6:52 AM