# Question on http\_request()

**URL:** <https://forum.dfinity.org/t/question-on-http-request/2637>\
**Category:** Developers\
**Created:** [April 30, 2021, 3:20am UTC](https://forum.dfinity.org/t/question-on-http-request/2637 "2021-04-30T03:20:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hazel](https://forum.dfinity.org/u/Hazel)\
**Post date:** [April 30, 2021, 3:20am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/1 "2021-04-30T03:20:06Z")

</div>

Looking at [Introducing a new approach to handling HTTP requests and serving assets :: Internet Computer](https://sdk.dfinity.org/docs/http-middleware.html#_enabling_canisters_to_respond_to_http_requests)

Can Motoko canisters implement the http\_request() method?

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [April 30, 2021, 4:26am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/2 "2021-04-30T04:26:21Z")

</div>

Yes you definitely can! That’s actually what our asset canister is doing - it’s just a Motoko canister implementing `http_request` that we include with the SDK.

I’m hoping we can create a Vessel package to make it really easy to add asset functionality to canisters and also open source our SDK codebase soon so I can start linking to relevant code

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [April 30, 2021, 6:17am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/3 "2021-04-30T06:17:28Z")

</div>

I find it a bit confusing how this all used to work, could you help me please?

1. Accessing the canister through `<CANISTER_ID>.ic0.app` calls the asset canisters `retrieve()` function without arguments
2. This returns a blob which consists of a `index.js` file that contains some JS and static HTML → the so called `bootstrap` code
3. Executing this `bootstrap` code then did the following things  
3.1 create worker  
3.2 polyfill `window.ic`  
3.3 call `retrieve` again, but this time with `index.js` as an argument  
3.4 pass control of the DOM and the page to the newly fetched `index.js` from `3.3`

Is this correct?

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [April 30, 2021, 6:20am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/4 "2021-04-30T06:20:02Z")

</div>

Oh and is this the middleware server?

[https://docs.rs/tiny\_http/0.8.1/tiny\_http/](https://docs.rs/tiny_http/0.8.1/tiny_http/)

Isn’t the middleware server a single point of failure and centralization?

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [April 30, 2021, 10:58am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/5 "2021-04-30T10:58:43Z")

</div>

I don’t think we have `retrieve()` anymore, nor the bootstrap code. Are you looking at old versions of the asset canister?

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [April 30, 2021, 11:15am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/6 "2021-04-30T11:15:08Z")

</div>

Yes, I’m reading the article @Hazel linked where the workings of the old assets canister are explained

---

<div class="post-metadata">

**Author:** ![Hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hazel](https://forum.dfinity.org/u/Hazel)\
**Post date:** [April 30, 2021, 1:31pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/7 "2021-04-30T13:31:53Z")

</div>

@anon74414410 - Do you have any examples you could share 😅 ?

Or, is it as-simple-as just implementing the interfaces here in Motoko  
(and setting the type to asset?)

> <https://github.com/dfinity/agent-rs/blob/b9c0b28c2dfd7fbd2a654874f8fd9a9773fa0c8d/ic-utils/src/interfaces/http_request.rs>

and doing something like…

```auto
public shared func http_request(req : HttpRequest) : async HttpResponse {
  // Do stuff w/ req, build response
};

```

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [April 30, 2021, 2:09pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/8 "2021-04-30T14:09:40Z")

</div>

Make sure you mark it as a `query` method, but yes, it is as simple as that. Try this as an (untested) starting point:

```auto
shared actor {

  type HeaderField = (Text, Text);

  type HttpRequest = {
    method: Text;
    url: Text;
    headers: [HeaderField];
    body: Blob;
  };

  type HttpResponse = {
    status_code: Nat16;
    headers: [HeaderField];
    body: Blob;
  };

  public query func http_request(request: T.HttpRequest): async T.HttpResponse {
    …
  }
}

```

(I left out the streaming part, for simple experiments this should not be needed. Thanks to Candid subtyping, it should work fine.)

Note that Motoko’s support for string parsing, e.g. for headers, is limited. Community-provided libraries are welcome!

---

<div class="post-metadata">

**Author:** ![Hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hazel](https://forum.dfinity.org/u/Hazel)\
**Post date:** [April 30, 2021, 2:10pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/9 "2021-04-30T14:10:56Z")

</div>

> [@nomeata](#):
>
> `http_request`

Will give this a shot after work today. I’m super excited for this, great job everyone!!!

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [May 4, 2021, 2:44pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/10 "2021-05-04T14:44:13Z")

</div>

> [@nomeata](#):
>
> ```auto
> ublic query func http_request(request: T.HttpRequest): async T.HttpResponse {
> …
> 
> ```

Hi @Hazel

Any luck with this?

I’m also interested but if you got this working there’s no point for me to reinvent the wheel.

Can you share your code please?

---

<div class="post-metadata">

**Author:** ![Hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hazel](https://forum.dfinity.org/u/Hazel)\
**Post date:** [May 8, 2021, 4:54am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/11 "2021-05-08T04:54:44Z")

</div>

> <https://gist.github.com/SuddenlyHazel/26613b42974b88dade60a9db6265fe32>

Working example!

---

<div class="post-metadata">

**Author:** ![ComputerInternetMan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/computerinternetman/32/1848_2.png) [@ComputerInternetMan](https://forum.dfinity.org/u/ComputerInternetMan)\
**Post date:** [June 5, 2021, 10:08pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/12 "2021-06-05T22:08:55Z")

</div>

If I might humbly suggest…This is code that should be presented widely so folks can visualize.

‘’ Whoa, it works!! ‘’

Very flash Hazel.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [June 14, 2021, 1:49am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/13 "2021-06-14T01:49:06Z")

</div>

This looks great for hard coded or programmatic responses, but how do you read in your source files…like have some HTML that I want to return for a certain path…how can motoko load in that file and serve it up?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 14, 2021, 3:38am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/14 "2021-06-14T03:38:38Z")

</div>

I think the asset canister that gets created by `dfx` should allow you to do that.

---

<div class="post-metadata">

**Author:** ![wang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/wang/32/446_2.png) [@wang](https://forum.dfinity.org/u/wang)\
**Post date:** [June 14, 2021, 5:07am UTC](https://forum.dfinity.org/t/question-on-http-request/2637/15 "2021-06-14T05:07:50Z")

</div>

Has anyone gotten html templating to work with rust, or is building something for motoko?

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [June 14, 2021, 1:11pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/16 "2021-06-14T13:11:05Z")

</div>

Is DFX open sourced? It looks like the asset canisters just have a putAsset, getAsset, and http\_request() that serve the named assets. Seems like this would be an easy set of motoko function to just lift and put into your core canister rather than having a separate canister.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [August 27, 2021, 11:24pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/17 "2021-08-27T23:24:03Z")

</div>

Good question, I wonder myself whether DFX is open-sourced…

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [August 28, 2021, 9:13pm UTC](https://forum.dfinity.org/t/question-on-http-request/2637/18 "2021-08-28T21:13:08Z")

</div>

Dfx is not (yet?) open source, but the asset canister is since a few days: [GitHub - dfinity/certified-assets: A certified assets canister written in Rust.](https://github.com/dfinity/certified-assets)
