This week, you will be going through steps to handle POST requests with no data.
✅ Task:
[x] 1: Modify your function code so that it receives a header value called codename and uses it to name the image.
If an image isn't attached, return "Sorry! No image attached."
If an image is attached, return the output of the uploadFile() function
[x] 2: Commit your updated function code to bunnimage/index.js to the bunnimage branch.
[x] 3: Create a pull request that merges bunnimage onto main, and only merge the pull request when the bot approves your changes!
🚧 Test Your Work
To test your work, use Postman to send a POST request without an image attached. You should see a response similar to the below:
{
"body" : "Sorry! No image attached."
}
💡 Yay! This means the error was successfully caught.
1: Modify the Function
Handle Empty Data
Now we'll want to handle cases where the POST request's body happens to be empty. In your original module.exports function, before you parse the body, make sure it exists! Only then should you upload your file to blob storage.
❓ How do I catch empty POST requests?
Use an try-catch statement to catch when `parse-multipart` is unable to parse the empty body. If catches an error, set the `responseMessage` to "Sorry! No image attached." Otherwise, you can safely parse the body!
```js
let responseMessage = ""
try {
let password = // get the header called "codename"
// use parse-multipart to parse the body
// determine the file-type here!
responseMessage = await uploadFile((place your parsedBody here), (place the extension here), (place the "codename" here));
// fill the parameters in!
} catch(err) {
context.log("Undefined body image");
responseMessage = "Sorry! No image attached."
}
```
> 💡 Hint: `responseMessage` is what we're returning to the user as the output.
💡 Hint: You'll need to add another parameter to the uploadFile function since it now has to receive the body, extension, AND filename!
Headers
Headers are yet another way to pass on information in HTTP requests. Here's how to access them:
let the_header_value = req.headers['insert_header_name'];
In this case, the header value will be used to name our picture.
Week 3 Step 3 ⬤⬤⬤◯◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
Codename Blob + 3RR0R Handling
This week, you will be going through steps to handle POST requests with no data.
✅ Task:
codename
and uses it to name the image.uploadFile()
functionbunnimage/index.js
to thebunnimage
branch.bunnimage
ontomain
, and only merge the pull request when the bot approves your changes!🚧 Test Your Work
To test your work, use Postman to send a POST request without an image attached. You should see a response similar to the below:
1: Modify the Function
Handle Empty Data
Now we'll want to handle cases where the POST request's body happens to be empty. In your original
module.exports
function, before you parse the body, make sure it exists! Only then should you upload your file to blob storage.❓ How do I catch empty POST requests?
Use an try-catch statement to catch when `parse-multipart` is unable to parse the empty body. If catches an error, set the `responseMessage` to "Sorry! No image attached." Otherwise, you can safely parse the body! ```js let responseMessage = "" try { let password = // get the header called "codename" // use parse-multipart to parse the body // determine the file-type here! responseMessage = await uploadFile((place your parsedBody here), (place the extension here), (place the "codename" here)); // fill the parameters in! } catch(err) { context.log("Undefined body image"); responseMessage = "Sorry! No image attached." } ``` > 💡 Hint: `responseMessage` is what we're returning to the user as the output.