QuickCheck: Ajax Review
Review: GET vs. POST
Making Post Requests With Postman
When/Where: Wednesday 4:30pm, BAG154
Description: How did Jeremy hack our website with CSS and Javascript? Come find out! Likely there will be ponies.
Note: this is at a different time and location than usual!
There are two common ways to make AJAX requests to a server.
Recall how you would need to make API calls:
For GET requests, you can directly visit the URL on your browser and see the response.
For POST requests, that would be a bit more complicated. We've told
you to test your POSTs by creating fetch
calls with
FormData
. (See the slides below)
The following testing code is enough to make a POST request:
// data parameters gets appended in FormData for POST requests (not in the url!)
let params = new FormData();
params.append("location", "Equestira");
params.append("group", "CSE 154 Wonderlads");
params.append("members", "8");
// make a POST request to httpbin.org/post with parameters attached
fetch("https://httpbin.org/post", {method: "POST", body: params});
JS
Now there is an another way to test your APIs. Instead of venturing with the Chrome Developer tools or experimenting the URLs (which can be error prone at times), we can make calls with a dedicated program.
The simplest of the request can be performed with Postman. By typing the URL into the bar and clicking on Send will make a GET request to the server.
Example 1 - GET the course website (https://courses.cs.washington.edu/courses/cse154/19sp/)
You can also insert query parameters (the stuff that goes after the url) to your GET request. They go into the Params input boxes.
Example 2 - GET https://httpbin.org/get w/ params: pony=rainbowdash and pokemon=ponyta
Most of the time you will be looking at the Body tab to see the output from the server. Occasionally you will need to see if the server is sending the correct Content-Type header. The headers can be accessed from the Headers tab.
Can you do the following?
http://www.csszengarden.com/
) Admire their HTML source. (Solution)https://courses.cs.washington.edu/courses/cse154/webservices/postmantest/get.php
https://courses.cs.washington.edu/courses/cse154/webservices/postmantest/getwithparams.php
(Solution)
user
parameter of the value ponyta
. (Solution)rainbowdash
? (Solution)Building POST requests on Postman is no more difficult than creating GET requests. Select POST from the dropdown list to the left of the URL box. Enter a POST URL into the box and hit send. You've just made your first POST request on Postman!
Example 4 - Making a POST request to https://httpbin.org/post
Since POST requests uses FormData instead of query parameters, we need to switch to the body tab in Postman. Select form-data from the radio buttons and enter your parameters.
Example 5 - Making a POST request to https://httpbin.org/post with data: name=rainbowdash, coolness=20-percent-cooler, and postman=rocks!
Can you do the following?
https://courses.cs.washington.edu/courses/cse154/webservices/postmantest/post.php
https://courses.cs.washington.edu/courses/cse154/webservices/postmantest/postwithparams.php
.user
as
rainbowdash
.
(Solution)
ponyta
. Set the
password
FormData as ponyta.
(Solution)
Great work! Now it's time to try to replicate what you just did using JavaScript!
Note: One popular strategy for working with APIs is to use Postman to understand the API you are working with then duplicate the request with JavaScript.
We have created a starter project for you to work with. It contains all the HTML and CSS needed as well as a starter JS file.
All you should need to do is make a POST request to the API with rainbowdash's username and password as you did in Postman.
https://courses.cs.washington.edu/courses/cse154/webservices/postmantest/postwithparams.php