Giter Site home page Giter Site logo

easyhttp's Introduction

Project Status

This project is active and maintained by David Alpert.

EasyHttp

An easy to use HTTP client that supports:

  • HEAD, PUT, DELETE, GET, POST
  • Cookies
  • Authentication
  • Dynamic and Static Typing
  • XML, JSON and WWW-Url form encoded encoding/decoding
  • File upload both via PUT and POST (multipart/formdata)
  • Some other neat little features....

License

Licensed under Modified BSD (i.e. pretty much MIT).

For full License and included software licenses please see LICENSE.TXT

Please log all issues here: http://youtrack.codebetter.com/issues/EHTTP

Installation

You can either download the source and compile or use nuget at http://nuget.org. To install with nuget:

Install-Package EasyHttp

Documentation

The documentation can be found on the wiki.

Usage

Using static types

To post/put a customer to some service:

	var customer = new Customer(); 
	customer.Name = "Joe"; 
	customer.Email = "[email protected]";
	var http = new HttpClient();
	http.Post("url", customer, HttpContentTypes.ApplicationJson);

To get some data in JSON format:

	var http = new HttpClient();
	http.Request.Accept = HttpContentTypes.ApplicationJson;
	var response = http.Get("url");
	var customer = response.StaticBody<Customer>();
	Console.WriteLine("Name: {0}", customer.Name);

Using dynamic types

To post/put a customer to some service:

	var customer = new ExpandoObject(); // Or any dynamic type
	customer.Name = "Joe";
	customer.Email = "[email protected]";
	var http = new HttpClient();
	http.Post("url", customer, HttpContentTypes.ApplicationJson);

To get some data in JSON format:

	var http = new HttpClient();
	http.Request.Accept = HttpContentTypes.ApplicationJson;
	var response = http.Get("url");
	var customer = response.DynamicBody;
	Console.WriteLine("Name {0}", customer.Name);

Both in Static and Dynamic versions, hierarchies are supported.

Perform a get with parameters

To get some data from a service

   var http = new HttpClient();
   http.Get("url", new {Name = "test"});

Should translate to the following url being passed. url?Name=test the value will be urlencoded.

To get some data in JSon format.

   var http = new HttpClient();
   http.Request.Accept = HttpContentTypes.ApplicationJson;
   http.Get("url", new {Name = "test"});

Serialization / Deserialization Conventions

For serialization / deserialization, you can use pretty much any type of naming convention, be it Propercase, CamelCase, lowerCamelCase, with_underscores, etc. If for some reason, your convention is not picked up, you can always decorate the property with an attribute:

 
   [JsonName("mycustomname")] 
   public string SomeWeirdCombination { get; set; }

Credits

Copyright (c) 2010 - 2017 Hadi Hariri and Project Contributors

JsonFX: Licensed under MIT. EasyHttp uses the awesome JsonFX library at http://github.com/jsonfx

easyhttp's People

Contributors

aaronkor avatar borismod avatar chrismckee avatar dalpert-korewireless avatar davidalpert avatar davidwalker avatar graemebradbury avatar hhariri avatar jole78 avatar mikeedwardseduserv avatar minglei84 avatar nocfed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

easyhttp's Issues

How do you add an Authorization header containing a bearer token?

For example, how would I make a request like the one below?

HTTP GET https://example.com
Content-Type: application/json
Accept: application/json
Authorization: Bearer d352b45d-0e5b-4c2d-a10b-c7be8c7cd3ff

I would expect to be able to do something like,

using EasyHttp.Http;

var http = new HttpClient();
http.Request.ContentType = "application/json";
http.Request.Accept = "application/json";
http.Request.Authorization = "Bearer d352b45d-0e5b-4c2d-a10b-c7be8c7cd3ff";

var response = http.Get("https://example.com");

But there's no Authorization field exposed, which is odd. Am I missing it somewhere?

Example code for using EasyHttp in VB.NET

This is not a question or problem. I just have a guide to help out those who are interested to use this wonderful library in a VB.NET app. I installed version 1.7.0 from NuGet packages. Here is an inclusive example I wrote on how to run a POST request using EasyHttp:

Imports System.Dynamic
Imports EasyHttp.Http

Private Sub makePostRequest()
        Dim http As New HttpClient

        'If you wish to set the user agent
        http.Request.UserAgent = "My App"

        'If you wish to use an auth token
        http.Request.AddExtraHeader("Authorization", "Bearer d352b45d-0e5b-4c2d-a10b-c7be8c7cd3ff")

        'Your form values
        Dim customer As Object = New ExpandoObject()
        customer.fname = "Test"
        customer.lname = "123"

        'Your query parameters
        Dim route As Object = New ExpandoObject()
        route.x = "123"
        route.y = "456"

        http.Post("http://example.com", customer, HttpContentTypes.ApplicationXWwwFormUrlEncoded, route)
End Sub

Decouple file-access logic

Hello there,
I wanted to implement a simple progress-tracking and noticed that the logic to read the file, and write it to the request-stream is implemented several times, slightly differently, violating e.g. DRY or SoC.

  1. In the MultiPartStreamer class (here)
  2. In the HttpRequest class (here).

If you agree that this is a problem, how would you solve it?
My proposals:

  • A) Defining an interface like IFileDataProvider with a simple signature like WriteFileToStream(string path, Stream requestStream)
  • B) A parameter like Action<string, Stream, long> streamWriter

I see the following advantages of this approach:

  • Removes redundancy
  • Ensures SoC
  • Great flexibility: You don`t care if the file to upload is on your local computer, or a remote http resource

Upper casing the first letter of each parameter

I don't know if this can be set to false but the library is making the decision of upper casing the first letter of each parameter name in all requests.

Json example: {"Name":"John", "Age":25}
While it should look like this: {"name":"John", "age":25}

This gets a different response from what's expected.

SSL issue when running HTTPS request

Hi, I am running an issue when try to do a request that uses HTTPS URL. The Visual Studio IDE shows this error when I attempt to make the request:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The request was aborted: Could not create SSL/TLS secure channel.

From other articles, I have already put the following in my code, but to no avail:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

Can someone please tell me what is the problem and what code is missing to fix it? Thanks!

Help to see this exception,Frequently appear

HttpResponse response = httpClient.Post(Api.InsertAttendanceRecordApi, new { record, RecordImage = reocrdImage }, HttpContentTypes.ApplicationJson);

System.InvalidOperationException: This property cannot be set after writing has started.
at System.Net.HttpWebRequest.set_ContentLength(Int64 value)
at EasyHttp.Http.Abstractions.HttpWebRequestWrapper.set_ContentLength(Int64 value) in C:\BuildAgent\work\f8da234c6955617\src\EasyHttp\Http\Abstractions\HttpWebRequestWrapper.cs:line 38
at EasyHttp.Http.HttpRequest.SetupData() in C:\BuildAgent\work\f8da234c6955617\src\EasyHttp\Http\HttpRequest.cs:line 237
at EasyHttp.Http.HttpRequest.SetupBody() in C:\BuildAgent\work\f8da234c6955617\src\EasyHttp\Http\HttpRequest.cs:line 214
at EasyHttp.Http.HttpRequest.PrepareRequest() in C:\BuildAgent\work\f8da234c6955617\src\EasyHttp\Http\HttpRequest.cs:line 292
at EasyHttp.Http.HttpClient.ProcessRequest(String filename) in C:\BuildAgent\work\f8da234c6955617\src\EasyHttp\Http\HttpClient.cs:line 217
at EasyHttp.Http.HttpClient.Post(String uri, Object data, String contentType, Object query) in C:\BuildAgent\work\f8da234c6955617\src\EasyHttp\Http\HttpClient.cs:line 155

RobiniaDocs API Explorer

Maybe You will be interested in hosting API Explorer online:
https://www.robiniadocs.com/d/easyhttp/api/EasyHttp.Http.HttpClient.html

This is ready-to-go, so above link can be added to README.md for other people to see it.
Or using ready shields.io badge:

Static Badge

[![Static Badge](https://img.shields.io/badge/API%20Documentation-RobiniaDocs-43bc00?logo=readme&logoColor=white)](https://www.robiniadocs.com/d/easyhttp/api/EasyHttp.Http.HttpClient.html)

https://github.com/NeuroXiq/RobiniaDocs

Hope You will find RobiniaDocs useful for this and other projects

Regards
NeuroXiq

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.