Python +请求发送Payload + Headers

时间:2015-12-14 11:58:11

标签: python http scripting request

我试图从Web应用程序中获取一些数据,以便在报告中使用它是Python代码

import requests
import json
url2 ="https://admin.XXXXXbeta.net/#exec-reports"
#this is the data for the login part
 payload {"apiKey":"877070gEt8t8","username":"aXXXX@XXXXX.XXXXXbeta.net","password":"XXXXXXX","timestamp":"1449666522626"}

这是重新定位部分的数据

 Payload2 ={"dataClass":"WEB","dataType":"URL_CATEGORY","units":"TRANSACTIONS","startTime":1450047600000,"endTime":1450092611720}
 Url3 ="https://admin.XXXXXbeta.net/zsapi/v1/reportData/web"

 reqHed ={"Accept":"application/json, text/javascript, */*; q=0.01",
     "Accept-Encoding":"gzip, deflate",
     "Accept-Language":"en-US,en;q=0.8",
     "Connection":"keep-alive",
     "Content-Length":"160",
     "Content-Type":"application/json",
     "Cookie":"Cookies01",
     "Host":"admin.XXXXXbeta.net",
     "Origin":"https://admin.XXXXXXXXX.net",
     "Referer":"https://admin.XXXXXXXXX.net/",
     "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36",
     "X-Requested-With":"XMLHttpRequest"


      }
     with requests.Session() as Req:
     url ="https://admin.XXXXXXXXX.net/XXXXapi/v1/authenticatedSession"

     login = Req.post(url, data=payload)
     CookieVal = login.cookies['JSESSIONID']
     Cookies01=  "webTransactionData=%5B%7B%22offset%22%3A51%7D%2C%7B%22username%22%3A248%7D%2C%7B%22urlPath%22%3A404%7D%2C%7B%22policyReason%22%3A102%7D%2C%7B%22urlCategory%22%3A150%7D%2C%7B%22riskScore%22%3A65%7D%2C%7B%22threatCategory%22%3A150%7D%2C%7B%22bytesTotal%22%3A100%7D%2C%7B%22clientIp%22%3A100%7D%2C%7B%22serverIp%22%3A100%7D%2C%7B%22isSsl%22%3A100%7D%2C%7B%22clientTimeMs%22%3A148%7D%5D; _gat=1; _ga=GA1.2.1233310469.1444980581; JSESSIONID="+CookieVal+"; username=XXXXX@XXXXXX.XXXXXbeta.net; locale=en-US; default-dashboard=1"

     reqHed ={"Accept":"application/json, text/javascript, */*; q=0.01",
     "Accept-Encoding":"gzip, deflate",
     "Accept-Language":"en-US,en;q=0.8",
     "Connection":"keep-alive",
     "Content-Length":"160",
     "Content-Type":"application/json",
     "Cookie":Cookies01,
     "Host":"admin.XXXXXbeta.net",
     "Origin":"https://admin.XXXXXbeta.net",
     "Referer":"https://admin.XXXXXbeta.net/",
     "User-Agent":"mY user agent",
     "X-Requested-With":"XMLHttpRequest"}


     Arr = Req.post(Url3,data=Payload2,headers=reqHed )
     print (Arr.headers)

这是Out put:

{'Server':'XXXXX','Content-Type':'text / plain','Connection':'close','Transfer-Encoding':'chunked','X-FRAME-OPTIONS': 'SAMEORIGIN','日期':'星期一,2015年12月14日11:46:15 GMT'} 400 意外字符('d'(代码100)):预期有效值(数字,字符串,数组,对象,'true','false'或'null')  在[来源:ch.qos.logback.access.servlet.TeeServletInputStream@1df260b; line:1,column:2]

处理完成,退出代码为0

我认为问题可能是有效载荷没有以正确的顺序发送任何想法? 谢谢

1 个答案:

答案 0 :(得分:2)

请求docs

  

通常,您希望发送一些表单编码数据 - 非常类似于HTML   形成。为此,只需将字典传递给data参数即可。您的   数据字典将在请求时自动进行表单编码   是

您的内容类型标题为"Content-Type":"application/json",并且您正在发送表单编码数据。

如果您想以json格式发送数据,请使用以下内容:

import json
Arr = Req.post(Url3,data=json.dumps(Payload2),headers=reqHed )