-
Notifications
You must be signed in to change notification settings - Fork 14
/
net.html
135 lines (100 loc) · 10.7 KB
/
net.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<head>
<meta charset="utf-8">
<!-- Page title -->
<title>Unirest for .NET - Simplified, lightweight HTTP Request Library</title>
<!-- Gotsta have that favicon-->
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<!-- Viewport -->
<meta name="viewport" content="width=480">
<!-- Stylesheet -->
<link href="styles/library.css" rel="stylesheet" type="text/css" data-inprogress="" />
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet" />
</head>
<body>
<nav>
<div class="right">
<a href="https://www.mashape.com/jobs">Created by Mashape <span>(We're Hiring!)</span></a>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="mashape" data-size="small" data-related="mashape" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<iframe src="http://ghbtns.com/github-btn.html?user=mashape&repo=unirest-net&type=fork&size=small" allowtransparency="true" frameborder="0" scrolling="0" width="52" height="20"></iframe>
</div>
<a href="http://unirest.io"><span class="icon-chevron-left"></span> Go Back</a>
</nav>
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div style="padding:30px 30px 25px" class="documentation">
<div class="documentation"><h1>
<a id="user-content-unirest-for-net" class="anchor" href="#unirest-for-net" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Unirest for .Net</h1>
<p><a href="https://camo.githubusercontent.com/899e266a5194998ba3da232ee9bc97c48deab283/687474703a2f2f636c2e6c792f696d6167652f32503337335930393073324f2f496d616765253230323031352d31302d31322532306174253230392e34382e3036253230504d2e706e67" target="_blank"><img src="https://camo.githubusercontent.com/899e266a5194998ba3da232ee9bc97c48deab283/687474703a2f2f636c2e6c792f696d6167652f32503337335930393073324f2f496d616765253230323031352d31302d31322532306174253230392e34382e3036253230504d2e706e67" alt="" data-canonical-src="http://cl.ly/image/2P373Y090s2O/Image%202015-10-12%20at%209.48.06%20PM.png" style="max-width:100%;"></a></p>
<p><a href="http://unirest.io">Unirest</a> is a set of lightweight HTTP libraries available in multiple languages, built and maintained by <a href="https://github.com/Mashape">Mashape</a>, who also maintain the open-source API Gateway <a href="https://github.com/Mashape/kong">Kong</a>. </p>
<p>This is a port of the Java library to .NET.</p>
<h2>
<a id="user-content-installing" class="anchor" href="#installing" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Installing</h2>
<p>We're currently updating Nuget to point to the latest package. In the meantime, please download this entire unirest-net library and reference it in your project.</p>
<h2>
<a id="user-content-creating-request" class="anchor" href="#creating-request" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Creating Request</h2>
<p>So you're probably wondering how using Unirest makes creating requests in .NET easier, here is a basic POST request that will explain everything:</p>
<div class="highlight highlight-source-cs"><pre>HttpResponse<MyClass> jsonResponse = Unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>)
.header(<span class="pl-s"><span class="pl-pds">"</span>accept<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span>)
.field(<span class="pl-s"><span class="pl-pds">"</span>parameter<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>value<span class="pl-pds">"</span></span>)
.field(<span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>bar<span class="pl-pds">"</span></span>)
.asJson<MyClass>();</pre></div>
<p>Requests are made when <code>as[Type]()</code> is invoked, possible types include <code>Json</code>, <code>Binary</code>, <code>String</code>. If the request supports this, a body can be passed along with <code>.body(String)</code> or <code>body<T>(T)</code> to serialize an arbitrary object to JSON. If you already have a dictionary of parameters or do not wish to use seperate field methods for each one there is a <code>.fields(Dictionary<string, object> parameters)</code> method that will serialize each key - value to form parameters on your request.</p>
<p><code>.headers(Dictionary<string, string> headers)</code> is also supported in replacement of multiple header methods.</p>
<h2>
<a id="user-content-asynchronous-requests" class="anchor" href="#asynchronous-requests" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Asynchronous Requests</h2>
<p>Sometimes, well most of the time, you want your application to be asynchronous and not block, Unirest supports this in .NET with the TPL pattern and async/await:</p>
<div class="highlight highlight-source-cs"><pre>Task<HttpResponse<MyClass>> myClassTask = Unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>)
.header(<span class="pl-s"><span class="pl-pds">"</span>accept<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span>)
.field(<span class="pl-s"><span class="pl-pds">"</span>param1<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>value1<span class="pl-pds">"</span></span>)
.field(<span class="pl-s"><span class="pl-pds">"</span>param2<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>value2<span class="pl-pds">"</span></span>)
.asJsonAsync<MyClass>();</pre></div>
<h2>
<a id="user-content-file-uploads" class="anchor" href="#file-uploads" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>File Uploads</h2>
<p>Creating <code>multipart</code> requests with .NET is trivial, simply pass along a <code>Stream</code> Object as a field:</p>
<div class="highlight highlight-source-cs"><pre><span class="pl-k">byte</span>[] data = File.ReadAllBytes(<span class="pl-s"><span class="pl-pds">@"filePath"</span></span>);
HttpResponse<MyClass> myClass = Unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>)
.header(<span class="pl-s"><span class="pl-pds">"</span>accept<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span>)
.field(<span class="pl-s"><span class="pl-pds">"</span>parameter<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>value<span class="pl-pds">"</span></span>)
.field(<span class="pl-s"><span class="pl-pds">"</span>files<span class="pl-pds">"</span></span>, data)
.asJson<MyClass>();</pre></div>
<h2>
<a id="user-content-custom-entity-body" class="anchor" href="#custom-entity-body" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Custom Entity Body</h2>
<div class="highlight highlight-source-cs"><pre>HttpResponse<MyClass> myClass = Unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>)
.header(<span class="pl-s"><span class="pl-pds">"</span>accept<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span>)
.body(<span class="pl-s"><span class="pl-pds">"</span>{<span class="pl-cce">\"</span>parameter<span class="pl-cce">\"</span>:<span class="pl-cce">\"</span>value<span class="pl-cce">\"</span>, <span class="pl-cce">\"</span>foo<span class="pl-cce">\"</span>:<span class="pl-cce">\"</span>bar<span class="pl-cce">\"</span>}<span class="pl-pds">"</span></span>)
.asJson<MyClass>();</pre></div>
<h1>
<a id="user-content-request" class="anchor" href="#request" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Request</h1>
<p>The .NET Unirest library follows the builder style conventions. You start building your request by creating a <code>HttpRequest</code> object using one of the following:</p>
<div class="highlight highlight-source-cs"><pre>HttpRequest request = Unirest.<span class="pl-k">get</span>(String url);
HttpRequest request = Unirest.post(String url);
HttpRequest request = Unirest.put(String url);
HttpRequest request = Unirest.patch(String url);
HttpRequest request = Unirest.delete(String url);</pre></div>
<h1>
<a id="user-content-response" class="anchor" href="#response" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Response</h1>
<p>Upon recieving a response Unirest returns the result in the form of an Object, this object should always have the same keys for each language regarding to the response details.</p>
<ul>
<li>
<code>.Code</code> - HTTP Response Status Code (Example 200)</li>
<li>
<code>.Headers</code> - HTTP Response Headers</li>
<li>
<code>.Body</code> - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.</li>
<li>
<code>.Raw</code> - Un-parsed response body</li>
</ul>
<hr>
<p>Made with ♥ from the <a href="https://www.mashape.com/">Mashape</a> team</p>
</div>
<script type="text/javascript" src="//mashape.github.io/notification-bar/embed.js" async defer></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$('a img').parent().css('border-bottom', '0px')
</script>
<script type="text/javascript">
var analytics=analytics||[];analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.NET/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)))}},i=["identify","track","trackLink","trackForm","trackClick","trackSubmit","pageview","ab","alias","ready"];for(var s=0;s<i.length;s++)analytics[i[s]]=r(i[s])}; analytics.load("xsv6p66v1y");
</script>
<script src="script/script.js"></script>
</body>
</html>