CURL in python

You can use the standard library urllib or the third-party library Requests, but try urllib.

GET

get.py


import urllib.request
import json

url = 'https://petstore.swagger.io/v2/store/inventory'

try:
    with urllib.request.urlopen(url) as response:
        body = json.loads(response.read())
        headers = response.getheaders()
        status = response.getcode()

        print(headers)
        print(status)
        print(body)

except urllib.error.URLError as e:
     print(e.reason)
>python get.py
[('Date', 'Fri, 10 Apr 2020 10:25:52 GMT'), ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT'), ('Access-Control-Allow-Heade/python.exe c:/Users/hoge/Desktop/get.pyrs', 'Content-Type, api_key, Authorization'), ('Content-Type', 'application/json'), (ccess-Control-Allow-Methods', 'GET, POST, DELETE, PUT'), ('Access-Control-Allow-Headers', 'Content-Type, api_key, Aut'Connection', 'close'), ('Server', 'Jetty(9.2.9.v20150224)')]                        ', 'Jetty(9.2.9.v20150224)')]
200
{'sold': 51, 'string': 187, 'pending': 52, 'available': 671, 'Available': 1}

POST

post.py


import urllib.request
import json

url = 'https://petstore.swagger.io/v2/store/order'
req_header = {
    'Content-Type': 'application/json',
}
req_data = json.dumps({
  'id': 0,
  'petId': 0,
  'quantity': 0,
  'shipDate': '2020-04-10T10:11:13.419Z',
  'status': 'placed',
  'complete': True,
})

req = urllib.request.Request(url, data=req_data.encode(), method='POST', headers=req_header)

try:
    with urllib.request.urlopen(req) as response:
        body = json.loads(response.read())
        headers = response.getheaders()
        status = response.getcode()

        print(headers)
        print(status)
        print(body)

except urllib.error.URLError as e:
     print(e.reason)
>python post.py
[('Date', 'Fri, 10 Apr 2020 10:54:21 GMT'), ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT'), ('Access-Control-Allow-Headers', 'Content-Type, api_key, Authorization'), ('Content-Type', 'application/json'), ('Connection', 'close'), ('Server', 'Jetty(9.2.9.v20150224)')]
200
{'id': 890544, 'petId': 0, 'quantity': 0, 'shipDate': '2020-04-10T10:11:13.419+0000', 'status': 'placed', 'complete': True}

Recommended Posts

CURL in python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python