|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 觀察 GET / POST 的差別\n", |
| 8 | + "\n", |
| 9 | + "透過 postman 網站的測試觀察 GET 與 POST 之間的差別\n", |
| 10 | + "\n", |
| 11 | + "- https://docs.postman-echo.com/" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import requests\n", |
| 21 | + "from pprint import pformat" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "## GET request\n", |
| 29 | + "\n", |
| 30 | + "- 觀察回傳的內容\n", |
| 31 | + "- 觀察 URL" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 2, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "get_url = 'https://postman-echo.com/get'\n", |
| 41 | + "query = {\n", |
| 42 | + " 'name': 'afun',\n", |
| 43 | + " 'msg': 'A Foolish Consistency is the Hobgoblin of Little Minds'\n", |
| 44 | + "}\n", |
| 45 | + "\n", |
| 46 | + "get_resp = requests.get(get_url, params=query)" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 3, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "name": "stdout", |
| 56 | + "output_type": "stream", |
| 57 | + "text": [ |
| 58 | + "response text ('{\"args\":{\"name\":\"afun\",\"msg\":\"A Foolish Consistency is the Hobgoblin of '\n", |
| 59 | + " 'Little '\n", |
| 60 | + " 'Minds\"},\"headers\":{\"host\":\"postman-echo.com\",\"accept\":\"*/*\",\"accept-encoding\":\"gzip, '\n", |
| 61 | + " 'deflate\",\"user-agent\":\"python-requests/2.19.1\",\"x-forwarded-port\":\"443\",\"x-forwarded-proto\":\"https\"},\"url\":\"https://postman-echo.com/get?name=afun&msg=A+Foolish+Consistency+is+the+Hobgoblin+of+Little+Minds\"}')\n", |
| 62 | + "=======================================================================================\n", |
| 63 | + "original URL - https://postman-echo.com/get\n", |
| 64 | + "GET URL - https://postman-echo.com/get?name=afun&msg=A+Foolish+Consistency+is+the+Hobgoblin+of+Little+Minds\n" |
| 65 | + ] |
| 66 | + } |
| 67 | + ], |
| 68 | + "source": [ |
| 69 | + "print('response text', pformat(get_resp.text))\n", |
| 70 | + "print('='*87)\n", |
| 71 | + "print('original URL -', get_url)\n", |
| 72 | + "print('GET URL -', get_resp.url)" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "## POST request\n", |
| 80 | + "\n", |
| 81 | + "- 觀察回傳的內容\n", |
| 82 | + "- 觀察 URL" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 4, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "post_url = 'https://postman-echo.com/post'\n", |
| 92 | + "payload = 'A Foolish Consistency is the Hobgoblin of Little Minds'\n", |
| 93 | + "\n", |
| 94 | + "post_resp = requests.post(post_url, data=payload)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 5, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "name": "stdout", |
| 104 | + "output_type": "stream", |
| 105 | + "text": [ |
| 106 | + "response text ('{\"args\":{},\"data\":{},\"files\":{},\"form\":{},\"headers\":{\"host\":\"postman-echo.com\",\"content-length\":\"54\",\"accept\":\"*/*\",\"accept-encoding\":\"gzip, '\n", |
| 107 | + " 'deflate\",\"user-agent\":\"python-requests/2.19.1\",\"x-forwarded-port\":\"443\",\"x-forwarded-proto\":\"https\"},\"json\":null,\"url\":\"https://postman-echo.com/post\"}')\n", |
| 108 | + "=======================================================================================\n", |
| 109 | + "original URL - https://postman-echo.com/post\n", |
| 110 | + "GET URL - https://postman-echo.com/post\n" |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "source": [ |
| 115 | + "print('response text', pformat(post_resp.text))\n", |
| 116 | + "print('='*87)\n", |
| 117 | + "print('original URL -', post_url)\n", |
| 118 | + "print('GET URL -', post_resp.url)" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 6, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "query = {\n", |
| 128 | + " 'name': 'afun',\n", |
| 129 | + " 'msg': 'A Foolish Consistency is the Hobgoblin of Little Minds'\n", |
| 130 | + "}\n", |
| 131 | + "post_form_data_resp = requests.post(post_url, data=query)" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 7, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [ |
| 139 | + { |
| 140 | + "name": "stdout", |
| 141 | + "output_type": "stream", |
| 142 | + "text": [ |
| 143 | + "response text ('{\"args\":{},\"data\":\"\",\"files\":{},\"form\":{\"name\":\"afun\",\"msg\":\"A Foolish '\n", |
| 144 | + " 'Consistency is the Hobgoblin of Little '\n", |
| 145 | + " 'Minds\"},\"headers\":{\"host\":\"postman-echo.com\",\"content-length\":\"68\",\"accept\":\"*/*\",\"accept-encoding\":\"gzip, '\n", |
| 146 | + " 'deflate\",\"content-type\":\"application/x-www-form-urlencoded\",\"user-agent\":\"python-requests/2.19.1\",\"x-forwarded-port\":\"443\",\"x-forwarded-proto\":\"https\"},\"json\":{\"name\":\"afun\",\"msg\":\"A '\n", |
| 147 | + " 'Foolish Consistency is the Hobgoblin of Little '\n", |
| 148 | + " 'Minds\"},\"url\":\"https://postman-echo.com/post\"}')\n", |
| 149 | + "=======================================================================================\n", |
| 150 | + "original URL - https://postman-echo.com/post\n", |
| 151 | + "GET URL - https://postman-echo.com/post\n" |
| 152 | + ] |
| 153 | + } |
| 154 | + ], |
| 155 | + "source": [ |
| 156 | + "print('response text', pformat(post_form_data_resp.text))\n", |
| 157 | + "print('='*87)\n", |
| 158 | + "print('original URL -', post_url)\n", |
| 159 | + "print('GET URL -', post_form_data_resp.url)" |
| 160 | + ] |
| 161 | + } |
| 162 | + ], |
| 163 | + "metadata": { |
| 164 | + "kernelspec": { |
| 165 | + "display_name": "Python 3", |
| 166 | + "language": "python", |
| 167 | + "name": "python3" |
| 168 | + }, |
| 169 | + "language_info": { |
| 170 | + "codemirror_mode": { |
| 171 | + "name": "ipython", |
| 172 | + "version": 3 |
| 173 | + }, |
| 174 | + "file_extension": ".py", |
| 175 | + "mimetype": "text/x-python", |
| 176 | + "name": "python", |
| 177 | + "nbconvert_exporter": "python", |
| 178 | + "pygments_lexer": "ipython3", |
| 179 | + "version": "3.6.6" |
| 180 | + } |
| 181 | + }, |
| 182 | + "nbformat": 4, |
| 183 | + "nbformat_minor": 2 |
| 184 | +} |
0 commit comments