Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 42 additions & 54 deletions pages/serverless-functions/reference-content/code-examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ There are several ways to return a response from a handler function:

```python
def handle(event, context):
return {
"body": {
"message": "hello world"
},
"statusCode": 200,
"headers": {
"Content-Type": ["application/json"],
"your-header": "your-value"
}
}
return {
"body": {
"message": "hello world"
},
"statusCode": 200,
"headers": {
"Content-Type": ["application/json"],
"your-header": "your-value"
}
}
```

**Straight response without body:**

```python
def handle(event, context):
return {"message": "hello world"}
# or
return "my Message"
return {"message": "hello world"}
# or
return "my Message"
```

**Stringified response body (AWS Lambda):**
Expand All @@ -64,13 +64,13 @@ def handle(event, context):
import json

def handle(event, context):
return {
"body": json.dumps({"message": "hello world"}),
"headers": {
"Content-Type": ["application/json"],
},
"statusCode": 200,
}
return {
"body": json.dumps({"message": "hello world"}),
"headers": {
"Content-Type": ["application/json"],
},
"statusCode": 200,
}
```

### Getting data in Python
Expand All @@ -85,8 +85,8 @@ The following snippet returns the environment variable `ENV` you specified in th
import os

def handle(event, context):
env_variable = os.getenv('ENV')
return env_variable
env_variable = os.getenv('ENV')
return env_variable
```

### Using event objects in Python
Expand All @@ -103,10 +103,10 @@ You can pass information through your HTTP request with event objects. They are
```python

def handle(event, context):
# the event object is a Python dict
query_param = event['queryStringParameters']['parameter']
query_body = event['body']
# ...
# the event object is a Python dict
query_param = event['queryStringParameters']['parameter']
query_body = event['body']
# ...
```

Example of reading URL parameters:
Expand All @@ -115,12 +115,12 @@ Example of reading URL parameters:
curl https://myfunc/user/?id=1
```

Will fill the event with the following parameters:
will fill the event with the following parameters:
```python
event['path'] = "/user/"

event['queryStringParameters'] = {
"id": "1"
"id": "1"
}
```

Expand All @@ -130,8 +130,8 @@ CRON jobs share the same pattern as HTTP calls. You can find the information pas

```python
def handle(event, context):
cron_body = event['body']
# ...
cron_body = event['body']
# ...
```

### Connecting to HTTP services in Python
Expand All @@ -150,20 +150,20 @@ auth_token=os.getenv('X-AUTH-TOKEN') # You can pass tokens through os environmen
url={YOUR URL} # If you want a dynamic URL based on information sent to handle(), define the URL inside the function

def handle(event, context):
req = request.Request(url, method='GET')
req.add_header('X-Auth-Token',auth_token)
try:
req = request.Request(url, method='GET')
req.add_header('X-Auth-Token',auth_token)
try:
res = request.urlopen(req).read().decode()
except error.HTTPError as e:
res = e.read().decode()
except error.HTTPError as e:
res = e.read().decode()

return {
"body": json.loads(res),
"headers": {
"Content-Type": ["application/json"],
},
"statusCode": 200,
}
return {
"body": json.loads(res),
"headers": {
"Content-Type": ["application/json"],
},
"statusCode": 200,
}
```

**`POST` request example**
Expand Down Expand Up @@ -194,18 +194,6 @@ def handle(event, context):
},
"statusCode": 200,
}
try:
res = request.urlopen(req).read().decode()
except error.HTTPError as e:
res = e.read().decode()

return {
"body": json.loads(res),
"headers": {
"Content-Type": ["application/json"],
},
"statusCode": 200,
}
```
<Message type="note">
To use other libraries like `request`, you need to upload your project [with advanced methods](/serverless-functions/reference-content/deploy-function/) (recommended)
Expand Down