post requests are four types of transfer body

I. Introduction

  HTTP protocol specified in the data message to be submitted post body (entity-body) but the data protocol does not specify what must encoding used. HTTP protocol is ASCII-code transmission, the establishment of standardized application layer on top of another TCP / IP protocol. HTTP request is divided into three parts: the status line, request headers and a message body. Looks like this:

<method> <request-URL> <version> <headers> <entity-body>  

  Server typically acquired message body of the request is encoded according to the manner in which the request header (header) field in Content_Type, and then parses the message body.

Second, the browser form submission form

  1.form form common properties as follows:

  action: url address, the server receives the form data address 
  method: Submit http server method, typically post and GET 
  name: 
  the enctype: encoding type used when submitting the form data, default "application / x-www-form -urlencoded" If using a POST request, the value of the specified content-type header is the value requested. If you have a file upload form, encoding type requires the use of "multipart / form-data", type to complete the transfer file data.

  PS: encoding format to form enctype form data, Content-type of encoding format of the HTTP data transmission, to distinguish the two! ! !

  2. When the browser submits the form, it will perform the following steps:

  ① identify valid entry form elements form, as submissions

  ② build a form data set

  ③ encoding data as a content-type content-type attribute value based on the value of the form attribute enctype form

  ④ transmits data to the specified address according to url form action attribute in a form and method attributes

  3. Submit in different ways

  ① get: After the form data will be encodeURIComponent as arguments: name1 = value1 & name2 = value2 url comes back in, and then sent to the server, and displayed in the url?.

  ② post: enctype Default "application / x-www-form-urlencoded" encoded form data, key data is transmitted in the http request to the server; if enctype attribute is "multipart / form-data", places the message the form is sent to the server.

Three, four common coding

  1.application/x-www-form-urlencoded

  The most common post submitted by encoding data. Native form form browser, if enctype attribute settings, default submission data application / x-www-form-urlencoded encoding. for example:

   POST http://www.example.com HTTP/1.1    
  Content-Type:application/x-www-form-urlencoded;charset=utf-8   title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

  2.multipart/form-data

  In addition to traditional application / x-www-form-urlencoded form, the other is used to upload files form, which form the type multipart / form-data for example.:

   <form action="/upload" enctype="multipart/form-data" method="post">
       Username: <input type="text" name="username">
       Password: <input type="password" name="password">
       File: <input type="file" name="file">
       <input type="submit">
   </form>

  3.application/json

  4.text/xml

Four, post body transmission request four kinds of ways

  1. The body of the request is application / x-www-form-urlencoded

  Form: requests.post (URL = '', Data = { 'key1': 'VALUE1', 'key2': 'value2'}, = {headers 'the Type-the Content': 'multipart / form-Data'}) 
  Requests sending a request to support post form form form, just to the parameters configured to request a dictionary, and then passed requests.post () of the dataparameters can be
import requests
url = "http://httpbin.org/post"
data = {"key1":"value1","key2":"value2"}
headers = {"Content-type":"application/x-www-form-urlencoded"}
r = requests.post(url=url,data=data,headers=headers)
print(r.text)

 

  由上图可以看到,请求头中的Content-Type字段已设置为application/x-www-form-urlencoded,且data = {'key1': 'value1', 'key2': 'value2'}以form表单的形式提交到服务端,服务端返回的form字段即是提交的数据。

  2.请求正文是multipart/form-data

  形式:

requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'multipart/form-data'})
from requests_toolbelt import MultipartEncoder
import requests
url="http://httpbin.org/post"
m = MultipartEncoder(fields={"field0":"value","field1":"value"})     # 删掉fields效果一样
headers = {"Content-Type":m.content_type}
r = requests.post(url=url,data = m,headers=headers)
print(r.text)

  

  3.请求正文是raw

  形式:

  ① 传入xml格式文本(可扩展性语言:Extensible markup language)

requests.post(url='',data='<?xml  ?>',headers={'Content-Type':'text/xml'})

  ② 传入json格式文本

requests.post(url='',data=json.dumps({'key1':'value1','key2':'value2'}),headers={'Content-Type':'application/json'})
或:requests.post(url='',json={{'key1':'value1','key2':'value2'}},headers={'Content-Type':'application/json'})

  for example:

import requests
import json
url = "http://httpbin.org/post"
j = {"key1":"value1","key2":"value2"}
data = json.dumps(j)  # json模块下的dumps函数将dict转换为str
headers = {"Content-type":"application/json"}
r = requests.post(url=url,data=data,json=j,headers=headers)
print(r.text)

  PS:脚本中dumps不要写成dump,否则会报缺失fp(类文件指针)的错误

  python中json.dumps()和json.dump()的区别:      https://www.cnblogs.com/fengff/p/11008353.html

 

  4.请求正文是binary

  形式:

requests.post(url='',files={'file':open('test.xls','rb')},headers={'Content-Type':'binary'})
import requests
url = "http://httpbin.org/post"
files = {"file":open("test.xlsx","rb")}
headers = {"Content-type":"binary"}
r = requests.post(url=url,files=files,headers=headers)
print(r.text)

   另外,requests也支持multipart方式发送post请求,只需将一文件传给requests.post()的参数files即可。 

import requests
url = "http://httpbin.org/post"
files = {"file":open("report.txt","rb")}
r = requests.post(url=url,files=files)
print(r.text)

PS:为了避免文件路径的问题,建议文件直接放在当前py文件的同一级目录下。

 

 

 


Guess you like

Origin www.cnblogs.com/huainanhai/p/12026726.html