简体   繁体   中英

Python + Requests sending the Payload + Headers

i am trying to get some data from a Web application to use it in reporting here is the Python code

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"}

this is the data for the reprting part

 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)

and here is the Out put :

{'Server': 'XXXXX', 'Content-Type': 'text/plain', 'Connection': 'close', 'Transfer-Encoding': 'chunked', 'X-FRAME-OPTIONS': 'SAMEORIGIN', 'Date': 'Mon, 14 Dec 2015 11:46:15 GMT'} 400 Unexpected character ('d' (code 100)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: ch.qos.logback.access.servlet.TeeServletInputStream@1df260b; line: 1, column: 2]

Process finished with exit code 0

i think that the problem can be that the payload are not sent in the correct order any idea ? thanks

Requests docs

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made

Your content type header is "Content-Type":"application/json" and you're sending form-encoded data.

If you want to send your data in the json format, use something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM