none
Rest Request Info RRS feed

  • Question

  • I'm super newb to REST. I watched a tut on REST and created a client that performs a GET. So how do I make the other requests? Do I just need to change one line here?

    public string GetRequest()
            {
                try
                {
                    string Response = string.Empty;
                    HttpWebRequest Request = WebRequest.Create(_EndPoint) as HttpWebRequest;
                    //HttpWebRequest Request = new (HttpWebRequest)WebRequest.Create(_EndPoint);
    
                    Request.Method = _HTTPMethod.ToString();
    
                    using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new Exception("Error: " + response.StatusCode.ToString());
                        }
    
                        using (Stream responsestream = response.GetResponseStream())
                        {
                            if (responsestream != null)
                            {
                                using (StreamReader reader = new StreamReader(responsestream))
                                {
                                    Response = reader.ReadToEnd();
                                }
                            }
                        }
                    }
    
                    return Response;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

    I think this is the only line I need to change right?

    HttpWebRequest Request = WebRequest.Create(_EndPoint) as HttpWebRequest;

    Sorry I'm super newb. I think you pass in the info for like a update or adding stuff in the url right? I'm so newb

    Friday, October 13, 2017 8:08 PM

Answers

  • I found the answer:

    Replace:

    Request.Headers[HttpRequestHeader.ContentType] = "application/json";

    With:

    Request.Accept = "application/json";


    • Edited by old_School Tuesday, October 17, 2017 6:00 PM
    • Marked as answer by old_School Tuesday, October 17, 2017 6:00 PM
    Tuesday, October 17, 2017 5:59 PM

All replies

  • Hello old_School,

    Based on my understanding for your description, I think you want to make many handlers for performing HTTP GET requests. If I understand right, you just need to add a parameter "string _EndPoint" for your function declaration , then to reuse the function with different url string to do these GET request operation to get the response string.

    Any concern, please feel free to let meknow. Hope it helps.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, October 16, 2017 7:21 AM
    Moderator
  • I have changed my code to this but I'm getting an error:

    public string PostRequest(string Email, string Data) { try { string Response = string.Empty; string url = _EndPoint + "?_queryFilter=userName+eq+" + '"' + Email + '"'; string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password)); HttpWebRequest Request = WebRequest.Create(url) as HttpWebRequest; Request.Headers[HttpRequestHeader.ContentType] = "application/json"; //Request.Headers.Add("Authorization", "Basic " + svcCredentials); Request.Method = _HTTPMethod.ToString(); using (var client = new WebClient()) { //Use Json IdentityDetails Object later Response = client.UploadString(url, "POST", Data); } return Response; } catch (Exception ex) { return ex.ToString(); } }

    Exception:

    The 'Content-Type' header must be modified using the appropriate property or method.\r\nParameter name: name"

    Tuesday, October 17, 2017 5:54 PM
  • I found the answer:

    Replace:

    Request.Headers[HttpRequestHeader.ContentType] = "application/json";

    With:

    Request.Accept = "application/json";


    • Edited by old_School Tuesday, October 17, 2017 6:00 PM
    • Marked as answer by old_School Tuesday, October 17, 2017 6:00 PM
    Tuesday, October 17, 2017 5:59 PM