[python] REST API 사용법

somnode·2020년 9월 24일
0
post-custom-banner

https://httpbin.org/의 REST API 스펙을 테스트하는 예제입니다.

  • Python version: 3.8

HTTP Methods

DELETE /delete
import json, requests

url = 'https://httpbin.org/delete'
r = requests.delete(url, headers={"Content-Type": "application/json"})
print(r.json())
GET /get
import json, requests

url = 'https://httpbin.org/get'
r = requests.get(url, headers={"Content-Type": "application/json"})
print(r.json())
POST /post
import json, requests

url = 'https://httpbin.org/post'
r = requests.post(url, headers={"Content-Type": "application/json"})
print(r.json())

Auth

GET /basic-auth/{user}/{passwd}
import json, requests
import base64

userPass = 'somuser:sompass'.encode()
b64Val = base64.b64encode(userPass).decode("utf-8")

url = 'https://httpbin.org/basic-auth/somuser/sompass'
r = requests.get(url, headers={"Content-Type": "application/json", "Authorization": "Basic %s" % b64Val})
print(r.json())

Request inspection

GET /ip
import json, requests

url = 'https://httpbin.org/ip'
r = requests.get(url, headers={"Content-Type": "application/json"})
print(r.json())

Dynamic data

GET /base64/{value}
import json, requests
import base64

text = 'Somm is awesome'.encode()
b64Val = base64.b64encode(text).decode("utf-8")

url = 'https://httpbin.org/base64/' + b64Val
r = requests.get(url, headers={"Content-Type": "text/html"})
print(r.text)
post-custom-banner

0개의 댓글