码迷,mamicode.com
首页 > Web开发 > 详细

web请求相关

时间:2019-01-05 21:32:01      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:word   als   microsoft   asc   知识点   ati   ali   sof   base   

请求数据类型

data
request.post(
url=xx,
data={k1:v1,k2:v2}
)
#数据: POST / http1.1\r\n....\r\n\r\nk1=v1&k2=v2
request.post(
url=xx,
data=json.dumps({k1:v1,k2:v2})
)

#数据: POST / http1.1\r\n....\r\n\r\n{‘k1‘:‘v1,‘k2‘:‘v2‘}


 字节

request.post(
url=xx,
data=basdfasdf
)
#数据: POST / http1.1\r\n....\r\n\r\n‘asdfasdf‘

 json

request.post(
url=xx,
json={k1:v1,k2:v2}
)
#数据: POST / http1.1\r\nContent-type:application/json....\r\n\r\n{‘k1‘:‘v1,‘k2‘:‘v2‘}

问题:POST请求发数据

django:获取不到值?request.POST

发送数据格式:

方式一:
request.post(
url=xx,
data={k1:v1,k2:v2}
)
#数据: POST / http1.1\r\nContent-type:urlencode-form.......\r\n\r\nk1=v1&k2=v2

request.POST必然可以获取到值。

  • - content-type: urlencode-form
  • - 数据格式:k1=v1&k2=v2


方式二:

request.post(
url=xx,
json={k1:v1,k2:v2}
)
#数据: POST / http1.1\r\nContent-type:application/json....\r\n\r\n{‘k1‘:‘v1,‘k2‘:‘v2‘}

通过request.body

  • 字节 = {‘k1‘:‘v1,‘k2‘:‘v2‘}
  • 字节转换字符串
  • 反序列化字符串 -> 字典

request.POST必然不可以获取到值。

  • - content-type: urlencode-form
  • - 数据格式:k1=v1&k2=v2

知识点:

技术分享图片
chrome->
    Form Data(&连接):
        phone=861513125555&password=12312312312&oneMonth=1
        
        reqeusts.post(
            url=url,
            data={
                phone:123123123123,
                password:asdfasdf
            }
        )
    
    Request Payload(json格式):
        {"BaseRequest":{"Uin":981579400,"Sid":"zWvteTWqBop4heoT","Skey":"@crypt_2ccf8ab9_a710cf413c932e201987599558063c8e","DeviceID":"e358217921593270"},"Msg":{"Type":1,"Content":"test","FromUserName":"@60eef3f2d212721fda0aae891115aa7a","ToUserName":"@@6a5403f510a3192454ed1afebd78ec6033d5057c9038d7b943b201f0a74987d4","LocalID":"15300708105840758","ClientMsgId":"15300708105840758"},"Scene":0}
    
        reqeusts.post(
            url=url,
            json={
                phone:123123123123,
                password:asdfasdf
            }
        )
        如果含有中文
        reqeusts.post(
            url=url,
            data=bytes(json.dumps({
                phone:123123123123,
                password:asdfasdf
            },ensure_ascii=False),encoding=utf-8)
        )


firefox:
    表单数据:
        
    JSON:
知识点

序列化:

技术分享图片
# -*- coding: utf-8 -*-

"""
@Datetime: 2019/1/5
@Author: Zhang Yafei
"""
""""
示例一:
     requests.post(
            url=msg_url,
            json= {
                ‘k1‘:‘v1‘,
                ‘k2‘:‘垃圾‘
            }
        )
    json.dumps()     ->  unicode 

示例二:
     requests.post(
            url=msg_url,
            data= json.dumps({
                ‘k1‘:‘v1‘,
                ‘k2‘:‘垃圾‘
            },ensure_ascii=False)
        )
    json.dumps()     ->  unicode 

示例二:
     requests.post(
            url=msg_url,
            data= bytes(json.dumps({
                ‘k1‘:‘v1‘,
                ‘k2‘:‘垃圾‘
            },ensure_ascii=False),encoding=‘utf-8‘)
        )
    json.dumps()     ->  unicode 
"""

import json

data_dict = {username:张亚飞, password:12132313213}

result = json.dumps(data_dict)
result2 = json.dumps(data_dict, ensure_ascii=False)
result3 = bytes(json.dumps(data_dict, ensure_ascii=False),encoding=utf-8)
print(result)
print(result2)
print(result3)

# {"username": "\u5f20\u4e9a\u98de", "password": "12132313213"}
# {"username": "张亚飞", "password": "12132313213"}
# b‘{"
序列化

 

web请求相关

标签:word   als   microsoft   asc   知识点   ati   ali   sof   base   

原文地址:https://www.cnblogs.com/zhangyafei/p/10225977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!