30
loading...
This website collects cookies to deliver better user experience
curl https://pokeapi.co/api/v2/pokemon/castform
https://example.com/product?utm_param=123&src=abc&campaign=xyz
?
, and then they follow the format item=value
. Where there is more than one we separate them with a &
. Let's make a new request to the Data USA API in our terminal:curl https://datausa.io/api/data?drilldowns=State&measures=Population
drilldowns
with a value of State
, and the second is measures
with a value of Population
to get the population by state in the USA. If you change the value of drilldowns
to County
you'll get different (and much bigger) data back. Try it!curl -H 'Content-Type: application/json' -d '{"userId": 1, "title": "sample title", "body": "sample body"}' -X POST https://jsonplaceholder.typicode.com/posts
-H 'Content-Type: application/json'
is known as a header and provides more information with the request. In this case, we are telling our the API to interpret our data as JSON. We'll be talking more about headers soon, but know each one begins with -H
when making API requests from the terminal.-d '{"userId": 1, "title": "sample title", "body": "sample body"}'
is a JSON object with three properties - a userId
, a title
, and a body
. Data begins with -d
when using the terminal for API requests.-X POST
specifies that we are using the POST method. If omitted, this would be a GET request (and you can't send data with a GET request).curl -H 'Content-Type: application/json' -H 'Authorization: Token REPLACE_WITH_YOUR_KEY' -d '{"url": "https://static.deepgram.com/examples/interview_speech-analytics.wav"}' -X POST https://api.deepgram.com/v1/listen
fetch()
function. Create a HTML file with just a <script>
tag. Inside it type:// GET
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then((response) => response.json())
.then((json) => console.log(json))
// POST
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }),
})
.then((response) => response.json())
.then((json) => console.log(json))
node-fetch
you can use identical syntax to the browser. After running npm install node-fetch
, create and open an index.js
file:const fetch = require('node-fetch')
// GET
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then((response) => response.json())
.then((json) => console.log(json))
// POST
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }),
})
.then((response) => response.json())
.then((json) => console.log(json))
import requests
# GET
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
print(response.text)
# POST
myData = { 'title': 'foo', 'body': 'bar', 'userId': 1 }
response2 = requests.post('https://jsonplaceholder.typicode.com/posts', data = myData)
print(response2.text)