Giter VIP home page Giter VIP logo

simplerestclient's Introduction

Simple RESTful Client

Simple C# class to provide an easy way of accessing RESTful API's. Based on the code of @AdamNThompson, this version provide extras like the HTTP PUT verb, possiblity to add headers, new constructors and some little code improvements.

Use

Just create an instance of the RestClient class, assign the value of your endpoint (the endpoint is the URL of the REST service you are attempting to call), and call the MakeRequest method.

Basic GET call.

string endPoint = @"http://jsonplaceholder.typicode.com/posts";
var client = new RestClient(endPoint);
var json = client.MakeRequest();

More examples

using headers for Basic access authentication

.....

PUT example using System.Web.Script.Serialization and try catch

try
{
    string endPoint = @"http://jsonplaceholder.typicode.com/posts/1";
    
    dynamic data = new { 
        id = 1,
        title = "foo",
        body = "bar",
        userId = 1 
    };

    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(data);

    var client = new RestClient(endPoint, HttpVerb.PUT, "application/json", json);
    return client.MakeRequest();
}
catch (WebException webExcp) 
{
    Console.WriteLine("A WebException has been caught.");
    // Write out the WebException message.
    Console.WriteLine(webExcp.ToString());
    // Get the WebException status code.
    WebExceptionStatus status =  webExcp.Status;
    // If status is WebExceptionStatus.ProtocolError, 
    //   there has been a protocol error and a WebResponse 
    //   should exist. Display the protocol error.
    if (status == WebExceptionStatus.ProtocolError) {
        Console.Write("The server returned protocol error ");
        // Get HttpWebResponse so that you can check the HTTP status code.
        HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
        Console.WriteLine((int)httpResponse.StatusCode + " - "
           + httpResponse.StatusCode);
    }
}
catch (Exception e) 
{
    // Code to catch other exceptions goes here.
}

Note some services return an HTTP status code even when the request went through, for example when searching for a resource but none are found the server, instead of returning a response with data, may return 404 Not Found error. This errors must be handle inside the try catch.

simplerestclient's People

Contributors

d-mx avatar

Watchers

 avatar James Cloos avatar

Forkers

kowunnako

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.