# Pyfetch example

**URL:** <https://forum.anaconda.com/t/pyfetch-example/20316>\
**Category:** PyScript\
**Created:** [May 2, 2022, 1:59am UTC](https://forum.anaconda.com/t/pyfetch-example/20316 "2022-05-02T01:59:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmarelas](https://avatars.discourse-cdn.com/v4/letter/p/f5a800/32.png) [@pmarelas](https://forum.anaconda.com/u/pmarelas)\
**Post date:** [May 2, 2022, 1:59am UTC](https://forum.anaconda.com/t/pyfetch-example/20316/1 "2022-05-02T01:59:49Z")

</div>

Hi All,

I’m looking for an example of calling an external API using pyfetch. If I try this the network call is never made.

```auto
from pyodide.http import pyfetch

key = "1232rxnwenwewefWEF@r34d"
endpoint = "https://XXX/"
query = {'blah'}
headers = {'API-Key': key}
response = pyfetch(
    endpoint,
    method="POST",
    body={"query": query},
    headers=headers
)
print(str(response))

```

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![MARTIN\_DURANT](https://dub1.discourse-cdn.com/anaconda/user_avatar/forum.anaconda.com/martin_durant/32/67_2.png) [@MARTIN\_DURANT](https://forum.anaconda.com/u/MARTIN_DURANT)\
**Post date:** [May 3, 2022, 1:55pm UTC](https://forum.anaconda.com/t/pyfetch-example/20316/2 "2022-05-03T13:55:08Z")

</div>

`pyfetch` is an async function, so you need to use `await`:

```auto
response = await pyfetch(...)

```

You probably will want to actually read the response after that, which requires another `await`. Furthermore, you should ensure that your endpoint is configured to allow CORS from anywhere.

---

<div class="post-metadata">

**Author:** ![ek2660](https://avatars.discourse-cdn.com/v4/letter/e/00a5b5/32.png) [@ek2660](https://forum.anaconda.com/u/ek2660)\
**Post date:** [May 3, 2022, 2:58pm UTC](https://forum.anaconda.com/t/pyfetch-example/20316/3 "2022-05-03T14:58:09Z")

</div>

Here is a PR that adds a simple pyfetch example.

> <https://github.com/pyscript/pyscript/pull/151>
>
> \### Summary
> Add a basic asynchronus demo in pure python for using \`pyodide.http….pyfetch\` for making HTTP requests.
> 
> \### Description
> \`pyfetch\` is the \`pyodide\` alternative for pythons widely used \`requests\` module (or other modules like httpx/urllib), since they are currently not supported in \`pyodide\`.
> 
> This demo uses the \`pyodide\` wrapper around javascript \`fetch()\`, called \`pyfetch\` to make "GET, POST, PUT, DELETE" requests to show how it can be done. Basically allows interfacing with a REST API from \`pyscript\`.
> 
> It's a simple modification of the Hello World demo to make a few requests to the free https://jsonplaceholder.typicode.com API.
> 
> \### Reason
> See several issues and posts on anaconda forum asking about the same question. 
> Users are suprised that requests doesn't work and there are no existing simple python demos to show how it works.
> Especially for making POST requests with a body and headers, etc. 
> 
> Note: demo requires python\>=3.10.
> 
> \### Related issues:
> \#91 #111

I wanted to see how it works so I played around with it. However, the HTML is very basic, this is just to show how to make pyfetch work.

---

<div class="post-metadata">

**Author:** ![pmarelas](https://avatars.discourse-cdn.com/v4/letter/p/f5a800/32.png) [@pmarelas](https://forum.anaconda.com/u/pmarelas)\
**Post date:** [May 3, 2022, 10:14pm UTC](https://forum.anaconda.com/t/pyfetch-example/20316/4 "2022-05-03T22:14:44Z")

</div>

Thanks @ek2660 PR looks great.

@MARTIN_DURANT you are right about CORS. Not something we needed to consider with Jupyter workflows. Thanks

---

<div class="post-metadata">

**Author:** ![yashshingvi](https://avatars.discourse-cdn.com/v4/letter/y/00a5b5/32.png) [@yashshingvi](https://forum.anaconda.com/u/yashshingvi)\
**Post date:** [July 12, 2022, 4:18pm UTC](https://forum.anaconda.com/t/pyfetch-example/20316/5 "2022-07-12T16:18:54Z")

</div>

Can we have this inside a function?
