python requests POST to api - replicate cURL - python

trying to replicate a cURL request to an appliance API with python Requests
curl -qgsSkH "Content-Type: multipart/form-data"
--no-progress-bar
--header "X-Api-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-F "filename=#cygdrive/c/tmp/test.txt"
-F "options={\"application\":\"2\",\"timeout\":\"500\",\"priority\":\"0\",
\"profiles\":[\"win7-sp1\"],\"analysistype\":\"0\",\"force\":\"true\",
\"prefetch\":\"0\"}"
https://xxx.xxx.xxx.xxx:443/apis/v1.1.0/submissions
I have succefully authenticated to the API and can use the recieved token in the POST request. My current python function is shown below.
def api_file_submit(api_token, file_name):
login_headers = {'X-Api-Token': api_token, 'Accept': 'application/json', 'Content-Type': 'multipart/form-data'}
files = {"filename": open(file_name, 'rb')}
sub_options = {"application": "-1", "timeout": "240", "priority": "0", "profiles": ["Win7 Sp1"],
"analysistype": "0", "force": "false", "prefetch": "value"}
j = simplejson.dumps(sub_options)
data = {'options': j}
r = requests.post(api_submit, headers=login_headers, files=files, data=data, verify=False)
print "REQUEST HEADER: " + str(r.request.headers)
print "REQUEST BODY: " + str(r.request.body)
print "RESPONSE HEADER: " + str(r.headers)
print "RESPONSE CONTENT: " + str(r.content)
print "RESPONSE STATUS: " + str(r.status_code)
print "RESPONSE REASON: " + str(r.reason)
return
The API returns a HTTP 400, Bad Request when attempting to POST. below are the results of the function
REQUEST HEADER: {'Content-Length': '424', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'User-Agent': 'python-requests/2.11.1', 'Connection': 'keep-alive', 'X-Api-Token': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'Content-Type': 'multipart/form-data'}
REQUEST BODY: --63a06536e02c4479a846c5fe320cdf7f
Content-Disposition: form-data; name="options"
{"force": "false", "profiles": ["Win7 Sp1"], "priority": "0", "application": "-1", "prefetch": "value", "timeout": "240", "analysistype": "0"}
--63a06536e02c4479a846c5fe320cdf7f
Content-Disposition: form-data; name="filename"; filename="test.vbs"
x = msgbox("this is an test file", 0, "test file")
--63a06536e02c4479a846c5fe320cdf7f--
RESPONSE HEADER: {'Content-Length': '257', 'Expires': '0', 'Connection': 'close', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Date': 'Fri, 24 Mar 2017 17:11:47 GMT', 'X-Frame-Options': 'DENY', 'Content-Type': 'application/json;charset=ISO-8859-1'}
RESPONSE CONTENT: {"apis":{"#version":"v1.1.0","description":"Error parsing options, please check you have provided a file for upload and included 'options' in json format as part of request","errorCode":"BESUB1001","httpStatus":400,"message":"Could not parse input"}}
RESPONSE STATUS: 400
RESPONSE REASON: Bad Request
Process finished with exit code 0
What am I missing to properly format the request and submit the file, replicating the cURL request above?

opened the file, not as a dictionary, just got the handle
formatted options as JSON, and included in the "files"
def api_file_submit(api_token, file_name):
login_headers = {'X-Api-Token': api_token, 'Accept': '*/*'}
files = open(file_name, 'rb')
sub_options = {'application': '0', 'timeout': '240', 'priority': '0', 'profiles': ['win7-sp1'], 'analysistype': '2', 'force': 'false', 'prefetch': '1'}
submissionData = ('', json.dumps(sub_options), 'application/json')
files = {'options': submissionData, 'filename': files}
r = requests.post(api_submit, headers=login_headers, files=files, verify=False)

Related

How can i extract cookies like this?

Hello guys I am making a bot in python requests to register a site and verify phone number however when I want to extract cookies I only get it like this this is one of the cookies: {'__cf_bm': 'Y0T3GzPQqXsWAnmYUSNWoaEogoto8_OELyA_wo_FUqQ-1662189807-0-Adw17pUjv2YaGcp3ynR1CX3CvGY/4AMBCJWqCn0GIk2qbGXQPifeKXlo5IN+iaDCsqeXrTN32gNXpdybETEYT6U='}
I want to extract them like this for each cookie:
{
"name": "__cf_bm",
"value": "je.AFZpQXtMPL7oZPdgCV6Bo4UxCb3BqMYdu2L7Eol0-1662189210-0-ARq9RgTbX7ItgRQ6dBXYjEdvQS8/fP7JiVibVrHYspk37sUiN3mQOlnHBfkJdMG033inhnAIS92vthv8bLdeYYQ=",
"domain": ".yemeksepeti.com",
"hostOnly": false,
"path": "/",
"secure": true,
"httpOnly": true,
"sameSite": "no_restriction",
"session": false,
"firstPartyDomain": "",
"partitionKey": null,
"expirationDate": 1662191010,
"storeId": "firefox-default",
"id": 1
}
this is the reproducible for register and then get the cookies from requests:
import json
import time
import requests
import selenium.webdriver
import urllib3
import random
import string
import secrets
password_length = 13
password = secrets.token_urlsafe(password_length)
file1 = open("Original.txt", "w")
kacAdet = int(input('KaƧ adet hesap kaydetmek istiyorsunuz?'))
proxies = {
'http': '23.229.62.241:3128',
}
def random_char(char_num):
return ''.join(random.choice(string.ascii_letters) for _ in range(char_num))
email = random_char(4)
print(random_char(4))
for i in range(kacAdet):
cookies = {
'dhhPerseusGuestId': '1661866956.4391864905.SzayaOAaKf',
'_gcl_au': '1.1.2045638547.1656310028',
'_pxvid': '60553c6b-f5df-11ec-b676-644a4b4e486e',
'_ga_W5PM07D07L': 'GS1.1.1661866956.12.1.1661871822.0.0.0',
'_ga': 'GA1.2.302729117.1656310029',
'_tq_id.TV-276372-1.c7a5': 'b3e61a11d2f82260.1656310030.0.1661871797..',
'__ssid': '288a4fe9f40bd5024bd37c43ed7b18f',
'_fbp': 'fb.1.1656310031494.1660145579',
'device_token': 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleW1ha2VyLXZvbG8tZGV2aWNlLXlzLXRyIiwidHlwIjoiSldUIn0.eyJpZCI6Ijk2MzI1ZThhLTVjYmQtNDk3NC1iZmYzLTgzYjdjMWZhMDdiZSIsImNsaWVudF9pZCI6InZvbG8iLCJ1c2VyX2lkIjoidHI4anBkOHMiLCJleHBpcmVzIjo0ODA5OTEwMDQ0LCJ0b2tlbl90eXBlIjoiYmVhcmVyIiwic2NvcGUiOiJERVZJQ0VfVE9LRU4ifQ.qlV25H4fL5mN9uHVHK-0VGpC6UZziOMfxpY5EbDXRCPeitQY-dfks5CA60Qj59alrbna3Ew5SS96HZ7elMOCDWDk9Al3RSBDgCoZNhn10jVexrmqqVBebRZyF27qh49tb1WoGE5FzWvX0MvlZA-1fvXCciPsHlRuKzr6oKPr4iZ2ilRBye9YwClEdpnu2ymg19moJhZn8oDmVotNAnEOC4tsRC64nXHT14rnU3zNx6yds1eeIjImOM-1u99lxOjmbTuBkQJWNEUxucimnA2RUe3VmZKhFronpM46ChdZr0f8xU7hvxGCZ2NxSVlN-uN79oHYThIgcxbaP0NjPajRcg',
'ab.storage.userId.f767a324-7168-4b7e-8687-652a3ac864bd': '%7B%22g%22%3A%22TR_44270161%22%2C%22c%22%3A1661869740576%2C%22l%22%3A1661869740576%7D',
'ab.storage.deviceId.f767a324-7168-4b7e-8687-652a3ac864bd': '%7B%22g%22%3A%224c13c6a1-fbd2-04ec-5d84-b972ec0ba576%22%2C%22c%22%3A1656310045145%2C%22l%22%3A1656310045145%7D',
'_hjSessionUser_1543571': 'eyJpZCI6ImE2MGI0MTQ2LTAzMGQtNWUwOS05YjhmLWZmMGRjNGE4YjRlOCIsImNyZWF0ZWQiOjE2NTYzMTA3MDg3NzIsImV4aXN0aW5nIjp0cnVlfQ==',
'AppVersion': '0ff5617',
'_gid': 'GA1.2.267011508.1661808541',
'__cf_bm': 'tjx3_.dOiiolQhAxWstqrkSP1CKvO_S1N4p.0EeINK4-1661871791-0-AUZhgLbPnAeoeXTbqYZ9zWdoMEV/paQVlVDqhYwfMhjTEpAzR9DL6w/R9PJ/UunzV1kS0E5eHo3I23DtfqQbu2g=',
'dhhPerseusSessionId': '1661866956.9452682616.Q3sF0bSpb0',
'dhhPerseusHitId': '1661871837551.356435850558823550.x7vphunw0q',
'ab.storage.sessionId.f767a324-7168-4b7e-8687-652a3ac864bd': '%7B%22g%22%3A%226aa2e303-cb5c-e1cd-d63c-ae74f0ef9a28%22%2C%22e%22%3A1661873591540%2C%22c%22%3A1661869740577%2C%22l%22%3A1661871791540%7D',
'hl': 'tr',
'_pxhd': '7s9aESLpFRxZZKv6tpZw1ARL/WCcfDvZ/5vEGc7NJzvNODk0RH90lYQB4Eua5Kqza/RouLT9ZEDCwApCgb6LWA==:fdJxKPXZt-rd6WPHmuK4PmpAXFudob0AhmJ-XL04XjMoHApgWh/7FNngAW72wqaDqlEmfZDx6D2EV-3kuOsLqWb15qT-85pfWFSmnayR70k=',
'_px3': 'e298931c318130994efbdc870fa364efa1eab482a8e5234e8667c760ad79fba0:6SPR0+3825cPBG3QzlECszOACYSdPXJLRT/ifAegNKTC7Ky1is4UYFhA01ROdi9t5FPC67v5CjaqHtWa7tzTuQ==:1000:DVJEiHg7S1XrIagRXrM6VXHwU2ozpb8RE2lXnVttxjQFilQ+GEHfG07sM+9BqFSgi69SNwFOgo8/C3CALs2K5rm1G7jyBJ432cNubNkJx+jEy2YeDw5fFuUlytWWtZDt+qcrwX9wwFJi/j6POWqnm9j1aZuVEztpuZnSLs0D0QZBZV0qaOPaQwxJw9vLCsigsgKcWGAbfbRMvVKXx0Cevw==',
'pxcts': 'deb1b68a-2874-11ed-9c26-6c6e70485242',
'tooltip-reorder': 'true',
'_dc_gtm_UA-86673819-11': '1',
'_dd_s': 'logs=1&id=38095de6-1b84-4b17-a32c-c539cfaf0dca&created=1661871796050&expire=1661872741919',
'_pxff_fp': '1',
}
headers = {
'Host': 'www.yemeksepeti.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0',
'Accept': 'application/json',
'Accept-Language': 'tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3',
'Content-Type': 'multipart/form-data; boundary=---------------------------19743795514118722864129047497',
'X-Requested-With': 'XMLHttpRequest',
'X-Device-Fingerprint': 'db5f21349eff189ec4f7664477e01cd4',
'X-Device': 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleW1ha2VyLXZvbG8tZGV2aWNlLXlzLXRyIiwidHlwIjoiSldUIn0.eyJpZCI6Ijk2MzI1ZThhLTVjYmQtNDk3NC1iZmYzLTgzYjdjMWZhMDdiZSIsImNsaWVudF9pZCI6InZvbG8iLCJ1c2VyX2lkIjoidHI4anBkOHMiLCJleHBpcmVzIjo0ODA5OTEwMDQ0LCJ0b2tlbl90eXBlIjoiYmVhcmVyIiwic2NvcGUiOiJERVZJQ0VfVE9LRU4ifQ.qlV25H4fL5mN9uHVHK-0VGpC6UZziOMfxpY5EbDXRCPeitQY-dfks5CA60Qj59alrbna3Ew5SS96HZ7elMOCDWDk9Al3RSBDgCoZNhn10jVexrmqqVBebRZyF27qh49tb1WoGE5FzWvX0MvlZA-1fvXCciPsHlRuKzr6oKPr4iZ2ilRBye9YwClEdpnu2ymg19moJhZn8oDmVotNAnEOC4tsRC64nXHT14rnU3zNx6yds1eeIjImOM-1u99lxOjmbTuBkQJWNEUxucimnA2RUe3VmZKhFronpM46ChdZr0f8xU7hvxGCZ2NxSVlN-uN79oHYThIgcxbaP0NjPajRcg',
'Origin': 'https://www.yemeksepeti.com',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Referer': 'https://www.yemeksepeti.com/login/new?step=registration',
}
data = '-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="_target_path"\r\n\r\nhttps://www.yemeksepeti.com/\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[first_name]"\r\n\r\nmert\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[last_name]"\r\n\r\ndemir\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[email]"\r\n\r\nmertdemir' + email + str(i) + '#gmail.com\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[password]"\r\n\r\n'+str(password)+'\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[birthdate]"\r\n\r\n1999-10-01\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[terms_and_conditions_consent]"\r\n\r\nagreed\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[marketing_consent]"\r\n\r\nopt-in\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[marketing_sms_consent]"\r\n\r\nopt-in\r\n-----------------------------19743795514118722864129047497--\r\n'
response = requests.post('https://www.yemeksepeti.com/api/v1/customers/async_register', cookies=cookies,
headers=headers, data=data, verify=False)
my_cookies = requests.utils.dict_from_cookiejar(response.cookies)
print(my_cookies)
print(response.status_code)
Don't use dict_from_cookiejar but write own code to convert it.
for item in response.cookies:
print('name :', item.name)
print('value :', item.value)
print('domain:', item.domain)
print('path :', item.path)
# ... other attributes ...
print('---')
EDIT:
Eventually you can use __dict__ to get all attributes as dictionary.
for item in response.cookies:
print(item.__dict__)
You may also get original data as string and parse it on your own.
print( response.headers['Set-Cookie'] )
EDIT:
import json
all_items = []
for item in response.cookies:
all_items.append(tem.__dict__)
# --- write ---
text = json.dumps(all_items)
with open('data.json', 'w') as fh:
fh.write(text)
# --- read ---
with open('data.json') as fh:
text = fh.read()
all_items = json.loads(text)

Python script API post request suddenly has stopped working

import requests
import json
from woocommerce import API
url2 = "http://ssl.autovit.ro/api/open/account/adverts"
url4 = "https://www.autovit.ro/api/open/imageCollections"
wcapi = API(
I wont show this, of course
)
r = wcapi.get('products?category=1880')
r = r.json()
print("Request sent Successfull!")
#r_string=r.text
### Request de generare si capturare token client
url = "https://www.autovit.ro/api/open/oauth/token"
payload = """
------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"client_id\"\r\n\r\n576\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"client_secret\"\r\n\r\33333\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"grant_type\"\r\n\r\npassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"username\"\r\n\r\n55555\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"password\"\r\n\r\n5555\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--
"""
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'User-Agent': "PostmanRuntime/7.15.0",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "08d86f5e-fcdb-4fdb-a570-b044e1ce2a4a,98ba3d2d-7a6f-4eaf-9c85-4512febf51d5",
'Host': "www.autovit.ro",
'cookie': "PHPSESSID=8ppb0muarimpt14g10nnq9j6qi; user_id=3235571; id_token=eyJraWQiOiJDY3ZTY1ljRE5MS294RzRFMktPSTltcU5zczJxcmN4dENWaG5Xd2JYNlEwPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiIxZGZhNTA1ZS1kMzU1LTQ2NDEtOTA5My01ZTFmZDQ5MDgwMWUiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLmV1LXdlc3QtMS5hbWF6b25hd3MuY29tXC9ldS13ZXN0LTFfMTRNVnJ2bGhUIiwicGhvbmVfbnVtYmVyX3ZlcmlmaWVkIjpmYWxzZSwiY29nbml0bzp1c2VybmFtZSI6ImU5MTYxNzNlLTQyZGItNDExYy04MGRlLTZmNDI1MTFkMjQzZCIsImxvY2FsZSI6InJvIiwiYXVkIjoiNTE5Z2YycjQ2cDhoMWJxaW12Mm1kbWJzc2YiLCJldmVudF9pZCI6IjU4MzhjOWYwLTBkMGUtNDdkZC1iY2M3LTg4N2E0MTU1Mjc1MyIsInRva2VuX3VzZSI6ImlkIiwiYXV0aF90aW1lIjoxNjA3NTI4MTk1LCJleHAiOjE2MDc1MzE3OTUsImlhdCI6MTYwNzUyODE5NSwiZW1haWwiOiJtdWx0aWNvbWF1dG9AeWFob28uY29tIn0.HBalbgWl-EdXjQQT2NA1GkoVjapVixZ_YvREDCU6bEJhWrnXT2PLVE-5DN2uVdfwtI6uesTe4cn-x1MfyRD4g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VB8pUXOzEAMfOuO0D-jfEKkttIMEonHZLNzl24Z1-gZo6XtcbviIaKw5nVlJyi_AUxzXUOURSE9QmzgdD6dGaU5J6ZBeHDdIcenGTceixArhBhvF9PV7WoWMlGv63G6gbtuV89NZ5cjr1GgjW-pmnfGuxv6Po0PxQAzY8AdDoK6pfAkn8L4gpAvMNbvC0PPVXV1OrfnaN7QsJ8yA-QEwdrENSml81JVdZZVMaHBMkrnkqHyH32eqES1PIORFgk-y7Ypi6WbScE9C-YPl1cOa8M3tnt2SaOod9x3_Q4qfi89_jb4MfBmIPZNltBVS3pZex2E5q53mL4NxAzes3XeIAog2jiSq-JsuAQt7f4p3-mFO4KpE7_18TAmZwbgzgHtQ0r10o2iqQQpYQzf3PIH5sOMi04aRSG4Q3LEzg_1nwLTZaW6O9aDklmuEgnKDEKyQW1BlW-LN5ICXEZovkMaOoU_luDomT7jYLRUY6v4CcmmnWDR2m7D84gA-3THEYJuR-aVxlnFsN5K6GGUEyETT17qK4ONqWMPR5w_GYenfV6N6tT_1rleHu1tjt5kAkCwU1o5oXhP4Q3hArJta7Par0xzM_I-whHI5ikrMHI83hrMu8nLSZ65h28gSc21ld94guaU-zCKIjvCo6gTqoGwbPd7yxopXSYYTqZ_-WPwDytITqf_Swj6GBG0yIiAu7-Zt10wmKgzfWARJECy4befAOiQMxReIuPaPC6kirEwyxMYXxq39WRqOfr-q_JjCrjyHugxPNmZaivFggfy9bqapScJK96-XHCTTyya78X-xmdu6VQjJiyE95G013aoau-X2-nkAswsOQhKDW2DR4EbRh2kO0yO8RRt8YU8a1OoED1W_mIuU2JvRJgUwGQnDiTFaL54w2GRrUSNVrL89s7MWnxg.vjAOfmEXwwfyOgFIW2Ti5Q; is_logged=1; newrelic_cdn_name=CF",
'accept-encoding': "gzip, deflate",
'content-length': "677",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
rresponse = requests.request("POST", url,data=payload, headers=headers)
req=rresponse.text
print(req)
json_data = json.loads(rresponse.text)
token=(json_data.get('access_token'))
### End of first request
for i in r:
titlu=(i.get('name'))
cat_id="69"
city_id="6613"
lat="44.9578"
longi="25.5688"
tip="interior"
body_type="hatchback"
region_id="6"
delivery="1"
make="honda"
manufacturer="Nespecificat"
manufacturer_code="1234"
pret=(i.get('price'))
vat="1"
advertiser_type="business"
phone="0744000000"
color="blue"
engine_power="320"
engine_capacity="1996"
gearbox="manual"
transmission="front-wheel"
co2_emissions="177"
person = "Multicom Auto"
images = (i.get('images'))
preview = images[0]
imagine= (preview.get('src'))
imagine2 = str(imagine)
print(imagine2)
payload4 = "{\n \"1\": \""+imagine2+"\"\n}"
headers4 = {
'Authorization': "Bearer " + token ,
'Accept': "application/json",
'Version': "2",
'Content-Type': "application/json",
'User-Agent': "PostmanRuntime/7.15.0",
'Cache-Control': "no-cache",
'Postman-Token': "6f9f0c71-b854-4d7d-a7dd-a281b445eaef,868f6f2c-a87a-4e11-95e9-365972bb72ed",
'Host': "ssl.autovit.ro",
'cookie': "PHPSESSID=aq78pib4a5ei411mtvk7ts9jkq; user_id=3235571; is_logged=1; newrelic_cdn_name=CF",
'accept-encoding': "gzip, deflate",
'content-length': "99",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
response4 = requests.request("POST", url4, data=payload4, headers=headers4)
descriere=(i.get('description'))
descriere2=descriere.replace("\n", "")
descriere3=descriere2.replace('<div>', '')
descriere5=descriere3.replace('</div>', '')
print(titlu + " \n " +descriere5)
json_id= response4.json()
image_collection_id= (json_id.get('id'))
print(response4)
### Start of Api POST Request
headers2 = {
'Authorization': "Bearer " + token,
'Accept': "application/json",
'Version': "2",
'Content-Type': "application/json",
'User-Agent': "PostmanRuntime/7.15.0",
'Cache-Control': "no-cache",
'Postman-Token': "04927f0a-cf06-4cb5-b83e-4eed29d1efec,829a1557-066a-4115-b134-66da62274203",
'Host': "ssl.autovit.ro",
'cookie': "PHPSESSID=iqfgqqgnbb15pk6ovt8bqvemm5; user_id=3235571; is_logged=1; id_token=eyJraWQiOiJDY3ZTY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gkI9NVMch0DZ_Pa4NbNLBfdh7aeR8sx7Vzfoz5wFVg9wuIwzSUp8A1JSha_KzcECY8GbEuaHfF1j6mF6R_Pue45BWlYyfBIz0J5zEPo9EXMqd4cWbrci7GhvEdeY6GGQTUUatwGNDVgz99hfmyf23AOAFu0w4IOgV7TlZvpt79vwBfJ0BDJwG-9ep1gM1tq7qLX_naTR6aWBN9d4KlILN52C9W2mWaMhGVTNST6I147L69lcITdjReuZPEuldE6mTl-W5TiTN1HHNbEBIRLRdd1XzLMlPbd1S6h8Ilwc6jYrt0GnWpnmMDJYOynoIv0A0eb-Loo79tVwjvfaUPuQDR7yBGT3Bwzec7RbVC6FQSI6BI1TlM0OeGW6eQ-gipjBV2CP9UF4HbcDWz2ZNOjNmlNwkqu63f4oxm1StyJRRHiPe0BPnVgBTtWrdli1dpM-d5YeTALNxowayUfPAiXb4Ih6MpGgnHYei2Tu5otVZz-jEEGeao0bG9Du7LHOtwd2C5OAw1hRS6NCC4QExETUjRWVk0vawLLDVTMVouRn6fXyKKBaM2kf1oc2td1J692mbyQCtmvmwNSw.65LsJCAJgfXFE6loW_JnsQ; newrelic_cdn_name=CF",
'accept-encoding': "gzip, deflate",
'content-length': "1062",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
payload2 = "{\r\n \"title\":\" "+titlu+" \",\r\n \"category_id\":\" "+cat_id+" \",\r\n \"region_id\": \" "+region_id+" \",\r\n \"city_id\":\""+city_id+"\",\r\n \"coordinates\":{\r\n \"latitude\":\""+lat+"\",\r\n \"longitude\":\""+longi+"\"\r\n },\r\n \"params\":{\r\n \"type\":\""+tip+"\",\r\n \"delivery\":\""+delivery+"\",\r\n \"title\":\""+titlu+"\",\r\n \"make\":\""+make+"\",\r\n \"manufacturer\":\""+manufacturer+"\",\r\n \"manufacturer_code\":\""+manufacturer_code+"\",\r\n \"body_type\":\""+body_type+"\",\r\n \"color\":\""+color+"\",\r\n \"price\":{\r\n \"0\":\"price\",\r\n \"1\":"+pret+",\r\n \"currency\":\"RON\",\r\n \"gross_net\":\"gross\",\r\n },\r\n \"vat\":"+vat+",\r\n \"engine_power\":\""+engine_power+"\",\r\n \"engine_capacity\":\""+engine_capacity+"\",\r\n \"gearbox\":\""+gearbox+"\",\r\n \"transmission\":\""+transmission+"\",\r\n \"co2_emissions\":\""+co2_emissions+"\"\r\n },\r\n \"description\":\""+descriere5+"\",\r\n \"advertiser_type\":\""+advertiser_type+"\",\r\n \"image_collection_id\":\""+image_collection_id+"\",\r\n \"contact\":{\r\n \"person\":\""+person+"\",\r\n \"phone_numbers\":[\r\n \""+phone+"\"\r\n ]\r\n }\r\n}"
url9 = "https://www.autovit.ro/api/open/account/adverts"
headers9 = {
'Authorization': "Bearer" +" "+ token,
'Content-Type': "application/json",
}
response9 = requests.request("GET", url9, headers=headers9)
response9_json=response9.json()
response9_txt=str(response9_json)
if titlu in response9_txt:
print("Anuntul a fost deja adaugat!")
else:
payload2_json=json.dumps(payload2)
response2 = requests.request("POST", url2 ,json=payload2, headers=headers2)
print(response2.text)
This script is just doing a bunch of requests, then creates ads on that website through api. However it has just stopped working suddenly, anyone has any idea why it gives me "BadRequestException","message":"Empty payload". Been trying to fix it all night long but it just wont work. I am thinking maybe its the guys that have the websites changed the api a bit or its an error somewhere? I tried sending payload as json, as utf8 but its not working anymore, no matter how hard i try

How to upload a file using requests in Python

I am trying to upload a file via Python requests and i receive an error code 400 (Bad request)
#Update ticket with upload of CSV file
header_upload_file = {
'Authorization': 'TOKEN id="' + token + '"',
'Content-Type': 'multipart/form-data'
}
files = {
'name': 'file',
'filename': open(main_path + '/temp/test.txt', 'rb'),
'Content-Disposition': 'form-data'
}
response = requests.post(baseurl + '/incidents/number/' + ticket_number + '/attachments/', headers=header_upload_file, data=files, verify=certificate)
If i try via Postman this is successfull using following code.
url = "https://<url>"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\Users\\<filename>\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Authorization': "TOKEN id="3e9d095d-a47b-48b5-a0b8-ae8b8ad9ae74"",
'cache-control': "no-cache",
'Postman-Token': "bb155176-b1b8-47a6-8fb3-46f5740cf9e0"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
What do i wrong?
You should use the files parameter instead. Also, don't explicitly set Content-Type in the header so that requests can set it for you with a proper boundary:
header_upload_file = {
'Authorization': 'TOKEN id="' + token + '"'
}
response = requests.post(
baseurl + '/incidents/number/' + ticket_number + '/attachments/',
headers=header_upload_file,
files={'file': ('file', open(main_path + '/temp/test.txt', 'rb'), 'text/csv')},
verify=certificate
)

How to Redirect to another url after POST request to PARSE / GRAB data ? Python 3.x

I am sending a JSON post request to a URL with headers and para:
headers = {
'channel':'mobiApp',
'Content-Type': 'application/json; charset=UTF-8',
'Content-Length': '939',
'Cookie': 'xyzxcasd',
'User-Agent': 'okhttp/3.8.0'
}
url = 'https://api.xyz.com/account.jsp'
data = {'act':'login','class':'profile','prftcf':code1,'p':password1,'u':username1,'ver':'0.9.1'}
content = json.dumps(data)
print ("")
print (content)
print ("")
r = requests.post(url,headers=headers,data=content,allow_redirects=True)
z = r.text
(above is the request I am sending)
I am getting this as response:
{
"s": 0,
"err": "",
"errCode": "",
"status": "",
"ccnt": 1,
"em": "blueyes81382#yahoo.com",
"pid": "2436999645",
"bvUserToken": "ee936b7065353389878696fc7cc4d71a646174653d3230313730393137267573657269643d32343336393939363435"
}
What I want to do is redirect after this post request (if bvUSertoken exists) to some URL, i.e.: https://api.xyz.com/acount/summary, and print the response.
okay so i found a solution to this
p['Set-cookie'] parse cookie from previous response header
cookies = (p['Set-Cookie'])
headers1 = {
'channel':'mobiApp',
'Content-Type': 'application/json; charset=UTF-8',
'Cookie' : cookies,
'User-Agent': 'okhttp/3.8.0'
}
url = 'your redirect url'
urlx = url
parsing = requests.get(urlx,headers=headers1)

JSON POST call in Python not working

I am new to python & POST message.
I am trying to call an API with JSON POST message and expecting a JSON response but my initial code not able to make the call as required .
using the URL,Headers & Postdata in chrome browser POST extension works fine.
#!/usr/bin/python
import requests
import json
url = 'http://xxxxx:111/batches'
postdata = {
"active": "true",
"size": "2",
"ctr": {
"user": "Admin",
"id": "1234"}}
#headers = {'content-type': 'application/json',
#'Authorization': 'Basic xyz879jjkhhnm',
#'Accept-Encoding': '0'}
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0'}
print headers
post_call = requests.post(url, headers=headers, data=json.dumps(postdata))
print post_call, "POST call"
print post_call.text, "TEXT"
print post_call.content, "CONTENT"
post_call.status_code, "STATUS CODE"
Error:
{'Accept-Encoding': '0', 'Authorization': 'Basic xyz879jjkhhnm'}
<Response [500]> POST call
[{"code":"server_error","description":"com.sun.jersey.api.MessageException: A message body reader for Java class com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity,
and Java type class com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity, and MIME media type application/octet-stream was not found.
\nThe registered message body readers compatible with the MIME media type are:\napplication/octet-stream ->\n
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n com.sun.jersey.core.impl.provider.entity.FileProvider\n
com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider\n*/* ->\n com.sun.jersey.core.impl.provider.entity.FormProvider\n
com.sun.jersey.core.impl.provider.entity.StringProvider\n com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n
com.sun.jersey.core.impl.provider.entity.FileProvider\n com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n
com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.ReaderProvider\n com.sun.jersey.core.impl.provider.entity.DocumentProvider\n
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader\n com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader\n
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader\n com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General\n com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General\n com.sun.jersey.core.impl.provider.entity.EntityHolderReader\n
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General\n com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General\n
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy\n","errorValues":null}] TEXT
**Edit:1 (changing Headers seems to get ridoff Java error now ,but getting below error now)**
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0', 'content-type': 'application/json'}
New error:
{'content-type': 'application/json', 'Accept-Encoding': '0', 'Authorization': 'Basic xyz879jjkhhnm'}
<Response [400]> POST call
[{"code":"invalid_attribute_of_request","description":"Attribute value type is not Integer. actual value = (\"2\")","errorValues":null,"field":"size"}] TEXT
[{"code":"invalid_attribute_of_request","description":"Attribute value type is not Integer. actual value = (\"2\")","errorValues":null,"field":"size"}] CONTENT
Ok,as per the latest change Header change in (EDIT 1) i had to remove the double quotes from post data "SIZE",that solved my error.
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0', 'content-type': 'application/json'}
postdata = {
"active": "true",
"size": "2", --> Changed to "size": 2,
"ctr": {
"user": "Admin",
"id": "1234"}}

Categories

Resources