{"id":11808,"date":"2025-09-06T15:41:40","date_gmt":"2025-09-06T15:41:40","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11808"},"modified":"2025-09-06T15:48:30","modified_gmt":"2025-09-06T15:48:30","slug":"rust-and-reqwest","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/rust-and-reqwest\/","title":{"rendered":"Rust and reqwest"},"content":{"rendered":"<p>I&#8217;ve covered a few topics around Rust lately on this blog. Hopefully around technologies that are most likely to be used in many real world applications. This post is about one of the missing pieces &#8211; how do we call our web API&#8217;s\/services etc. <\/p>\n<p>In C# we have the HttpClient which is the usual type for such use cases. With Rust there are various options, but as the title suggests, we&#8217;re going to concentrate on reqwest.<\/p>\n<p>All we really need to do is supply a couple of crates to Cargo.toml, as you&#8217;ll have guessed, one is <em>reqwest<\/em>. The other is <em>tokio<\/em> because I&#8217;m waiting to use async\/await. So create yourself a project then update Cargo.toml to add these dependencies<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nreqwest = &quot;0.12.23&quot;\r\ntokio = { version = &quot;1.47.1&quot;, features = &#x5B;&quot;rt&quot;, &quot;rt-multi-thread&quot;, &quot;macros&quot;] }\r\n<\/pre>\n<p>Next up, open src\/main.rs (or create one) and let&#8217;s add a simply GET call<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#&#x5B;tokio::main]\r\nasync fn main() -&gt; Result&lt;(), reqwest::Error&gt; {\r\nlet result = reqwest::get(&quot;https:\/\/httpbin.org\/get&quot;)\r\n  .await?\r\n  .text()\r\n  .await?;\r\n\r\n  println!(&quot;{}&quot;, result);\r\n  Ok(())\r\n}\r\n<\/pre>\n<p>This is a &#8220;shortcut&#8221; to use a get method. <\/p>\n<p>The following is a longer form and is what we&#8217;d use for other HTTP methods, but I&#8217;m showing how we can generate a RequestBuilder (the type returned from <em>client.get<\/em>) and then send this and retrieve the response<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet client = reqwest::Client::new();\r\nlet result = client.get(&quot;https:\/\/httpbin.org\/get&quot;);\r\nlet result = result.send().await?.text().await?;\r\n<\/pre>\n<p>Other HTTP methods, such as POST, DELETE etc. can be created from the RequestBuilder, for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet post = client.post(&quot;https:\/\/httpbin.org\/post&quot;)\r\n  .body(&quot;hello world&quot;)\r\n  .header(&quot;Content-Type&quot;, &quot;text\/plain&quot;);\r\n\r\nlet result = post.send().await?.text().await?;\r\n<\/pre>\n<p><strong>JSON instead of plan text<\/strong><\/p>\n<p>Often we&#8217;ll want to deserialize to types, i.e. via JSON, so update Cargo.toml to lok like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;dependencies]\r\nreqwest = { version = &quot;0.12.23&quot;, features = &#x5B;&quot;json&quot;] }\r\ntokio = { version = &quot;1.47.1&quot;, features = &#x5B;&quot;rt&quot;, &quot;rt-multi-thread&quot;, &quot;macros&quot;] }\r\nserde = { version = &quot;1.0.219&quot;, features = &#x5B;&quot;derive&quot;] }\r\n<\/pre>\n<p>Now change main.rs to add this code to the start of the file<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuse serde::Deserialize;\r\n\r\n#&#x5B;derive(Deserialize)]\r\nstruct ApiResponse {\r\n    message: String,\r\n}\r\n<\/pre>\n<p>ApiResponse will represent our object and we use the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet client = reqwest::Client::new();\r\nlet result = client.get(&quot;https:\/\/your_api&quot;);\r\nlet result = result\r\n        .send()\r\n        .await?\r\n        .json::&lt;ApiResponse&gt;()\r\n        .await?;\r\n\r\nprintln!(&quot;{}&quot;, result.message);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve covered a few topics around Rust lately on this blog. Hopefully around technologies that are most likely to be used in many real world applications. This post is about one of the missing pieces &#8211; how do we call our web API&#8217;s\/services etc. In C# we have the HttpClient which is the usual type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-11808","post","type-post","status-publish","format-standard","hentry","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11808"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11808\/revisions"}],"predecessor-version":[{"id":11814,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11808\/revisions\/11814"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}