Compare HTTP GET / POST with cURL (command) and Python (programming)

HTTP GET/POST Get HTTP GET information from the server. Returns a JSON file etc. HTTP POST sends information to the server. Nothing in particular returns (success, failure may be returned)

cURL and python

For those who can use HTTP GET / POST with cURL (command) but don't know how to put it into programming (Python). Python uses the Python3 urllib library.

HTTP GET Multiple headers have also been added. "-H" is the cURL header.

curl -X GET "https://example.com/api/" -H "accept: application/json" -H "Content-Type: form"
from urllib.parse import urlencode
from urllib.request import urlopen, Request

url = "https://example.com/api/"

headers = {
    "accept" :"application/json",
    "Content-Type" :"application/x-www-form-urlencoded"
}

request= Request(url, headers=headers)

with urlopen(request) as response:
    body= response.read()
    print(body)

HTTP POST

Multiple data have been added. This data is sent to the server. "-D" is the cURL data.


curl -X POST "https://example.com/api/" -H "accept: application/json" -d "temperature=18" -d "operation_mode=auto"
from urllib.parse import urlencode
from urllib.request import urlopen, Request

url = "https://example.com/api/"

headers = {
    "accept" :"application/json"
}

request = Request(url, headers=headers)

data = {
    "temperature": "18",
    "operation_mode": "auto",
}

data = urlencode(data).encode("utf-8")

response = urlopen(request, data)

Afterword

I don't know the details of HTTP GET / POST, so I kept it to a minimum, but I think the coat itself is correct. Find out more about HTTP GET / POST elsewhere. I think that it will come out if you use API etc.

Recommended Posts

Compare HTTP GET / POST with cURL (command) and Python (programming)
Programming with Python and Tkinter
Sample of HTTP GET and JSON parsing with python of pepper
Grant an access token with the curl command and POST the API
POST variously with Python and receive with Flask
Get git branch name and tag name with python
Compare xml parsing speeds with Python and Go
Get only Response Header with curl and wget
Get Gmail subject and body with Python and Gmail API
If you get lost with HTTP redirects 301 and 302
Get all standard inputs used in paiza and competitive programming with int (python)
3. 3. AI programming with Python
Python programming with Atom
Competitive programming with python
HTTP communication with Python
Get date with python
Programming with Python Flask
Get comments on youtube Live with [python] and [pytchat]!
Get mail from Gmail and label it with Python3
[Python] Get user information and article information with Qiita API
Get in touch with functional programming in JavaScript or Python 3
* Android * [HTTP communication_2] HTTP communication with Flask (hit WebAPI [GET, POST])
Get country code with python
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Get Twitter timeline with python
POST json with Python3 script
Get Youtube data with python
Post to slack with Python 3
Get rid of dirty data with Python and regular expressions
Get thread ID with python
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Easy HTTP server with Python
Get started with Python! ~ ② Grammar ~
python with pyenv and venv
[python] Get quotient and remainder
Get stock price with Python
Get home directory with python
[Python x Zapier] Get alert information and notify with Slack
Get keyboard events with python
Get additional data to LDAP with python (Writer and Reader)
Network programming with Python Scapy
Get Alembic information with Python
Works with Python and R
Check the HTTP status code response with the curl command (#Linux #Shell)
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Get started with Python! ~ ① Environment construction ~
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Link to get started with python
[Python] font family and font with matplotlib
[Introduction to Python3 Day 1] Programming and Python
Scraping with Node, Ruby and Python
Get reviews with python googlemap api
Scraping with Python, Selenium and Chromedriver