zoukankan      html  css  js  c++  java
  • http 协议中post请求数据格式

    这篇文章也有详细说过:https://www.cnblogs.com/QiKa/p/12863127.html

    HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-body)中
    
    1、application/x-www-form-urlencoded 
    #这应该是最常见的 POST 提交数据的方式了。浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据
    如:Content-Type: application/x-www-form-urlencoded ; charset=utf-8
    
    2、multipart/form-data 
    使用表单上传文件时,必须让 form 的 enctyped 等于这个值
    如:
    POST http://www.example.com HTTP/1.1 
    Content-Type:multipart/form-data; boundary=---WebKitFormBoundaryrGKCBY7qhFd3TrwA ------WebKitFormBoundaryrGKCBY7qhFd3TrwA 
    Content-Disposition: form-data; name="text"
    title ------WebKitFormBoundaryrGKCBY7qhFd3TrwA 
    Content-Disposition: form-data; name="file"; filename="chrome.png" Content-Type: image/png
    
    3、application/json 
    application/json把它作为请求头,用来告诉服务端,消息主体是序列化后的 JSON 字符串
    
    4、text/xml 
    它是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范
    
    
    实例:------------------------------------------------------------
    Python在调用外部http请求时,post请求进行传 请求体body
    1、application/x-www-form-urlencoded
    import urllib
    url = "http://www.example.com"
    body_value = {"package": "com.tencent.lian","version_code": "66" }
    body_value = urllib.urlencode(body_value)
    request = urllib2.Request(url, body_value)
    request.add_header(keys, headers[keys])
    result = urllib2.urlopen(request ).read()
    
    2、multipart/form-data 
    需要利用python的poster模块,安装poster:pip install poster 
    代码:
    from poster.encode import multipart_encode 
    from poster.streaminghttp import register_openers 
    url = "http://www.baidu.com"
    body_value = {"package": "com.tencent.lian","version_code": "77" }
    register_openers()
    datagen, re_headers = multipart_encode(body_value)
    request = urllib2.Request(url, datagen, re_headers)
    # 如果有请求头数据,则添加请求头
    request .add_header(keys, headers[keys])
    result = urllib2.urlopen(request ).read()
    
    3、application/json
    import json
    url = "http://www.baidu.com"
    body_value = {"package": "com.tencent.lian","version_code": "77" }
    register_openers()
    body_value = json.JSONEncoder().encode(_value)
  • 相关阅读:
    小程序云函数 Error: errCode: -404011 cloud function execution error
    Fiddler+雷电模拟器进行APP抓包
    Vue优化常用技巧
    sftp常用命令整理
    React生命周期学习整理
    git 提交跳过检查
    漂亮的代码系列
    关于我系列
    架构系列
    深度学习系列
  • 原文地址:https://www.cnblogs.com/QiKa/p/13341367.html
Copyright © 2011-2022 走看看