locked
HELP – API Request RRS feed

  • Question

  • User-1664485818 posted

     

    Hi folks, I’m having a nightmare with api request.

    My code returns the following error “The remote server returned an error: (404) Not Found.”

    I cannot hit the API directly due to my IP address not being on the allow list, plus my IP address is dynamic, so debugging is pragmatic.

    I receive no errors when I compile my code, and the httpWebRequest request looks ok.

    RequestUri{https://www.check-mot.service.gov.uk/api/v1/mot-history/WR08SZE/TOYOTA}    

    Code below and documentation provided for hitting API, any help much appreciated as I am losing the will to live!! 

    private string RequestVehicleData()
            {
                string make = "";
                
                string registration = (string)(Session["regNo"]);
                make = txtmake.Text;
             
    
                //Make Request
                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";
    
    
                //Get Response
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    return result;
                }
            }

    Monday, July 18, 2016 2:27 PM

Answers

  • User36583972 posted

    Hi brucey,

    500 – request was sent without proper  Content-type header set

    You can try the following code to visit the Web API.

             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9601/api/Values");//
                request.Method = "Post";
                request.KeepAlive = true;
                request.ContentType = "appication/json";
                request.Headers.Add("Content-Type", "appication/json");
                //request.ContentType = "application/x-www-form-urlencoded";
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }
                Response.Write(myResponse);
    

    My code returns the following error “The remote server returned an error: (404) Not Found.”

    I suggest you read the examples which Web API provider and let Web API provider give your a test environment. Then, you can use some test tools.

    Testing Tools to test "ASP.NET Web API:

    http://forums.asp.net/t/1951516.aspx?Testing+Tools+to+test+ASP+NET+Web+API+

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2016 2:13 AM

All replies

  • User446859115 posted

    Can you access the URL using your browser? Are there any special credentials you need to provide to access the api's?

    Monday, July 18, 2016 11:10 PM
  • User36583972 posted

    Hi brucey,

    500 – request was sent without proper  Content-type header set

    You can try the following code to visit the Web API.

             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9601/api/Values");//
                request.Method = "Post";
                request.KeepAlive = true;
                request.ContentType = "appication/json";
                request.Headers.Add("Content-Type", "appication/json");
                //request.ContentType = "application/x-www-form-urlencoded";
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }
                Response.Write(myResponse);
    

    My code returns the following error “The remote server returned an error: (404) Not Found.”

    I suggest you read the examples which Web API provider and let Web API provider give your a test environment. Then, you can use some test tools.

    Testing Tools to test "ASP.NET Web API:

    http://forums.asp.net/t/1951516.aspx?Testing+Tools+to+test+ASP+NET+Web+API+

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2016 2:13 AM