Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests post json raw data

How to post RAW DATA using Resuests lib on python? I'm trying to login.

Json throwing Exception

TypeError: set(['"clienteLogin":{"Token":"b94261f7e6744380919d406f576110cc","Operador":"","IdUnidadeNegocio":5,"PalavraCaptcha":"","Senha":"muranga21","cadastro":"on","Email":"[email protected]"},"mesclarCarrinho":true,"Token":"b94261f7e6744380919d406f576110cc","IdUnidadeNegocio":5,"Operador":""}']) is not JSON serializable

Body raw(Postman)

{"clienteLogin":{"Token":"cbf36fb0e3de4b65ab0037303979a806","Operador":"","IdUnidadeNegocio":5,"PalavraCaptcha":"","Senha":"muranga21","cadastro":"on","Email":"[email protected]"},"mesclarCarrinho":true,"Token":"cbf36fb0e3de4b65ab0037303979a806","IdUnidadeNegocio":5,"Operador":""}

Code

import requests,json
import cookielib
from lxml import html



s = requests.session()
req1 = s.get("https://carrinho.extra.com.br/Checkout?ReturnUrl=http://www.extra.com.br#login")
content1 = req1.content
tree = html.fromstring(content1)
token = tree.xpath("//script")[0].text.split('"')[1]
data = {
'"clienteLogin":{"Token":"'+token+'","Operador":"","IdUnidadeNegocio":5,"PalavraCaptcha":"","Senha":"muranga21","cadastro":"on","Email":"[email protected]"},"mesclarCarrinho":true,"Token":"'+token+'","IdUnidadeNegocio":5,"Operador":""}'
 }
headers = {
'Content-Type':'application/json'
}

req3 = s.post("https://carrinho.extra.com.br/Api/checkout/Cliente.svc/Cliente/Login",json.dumps(data),headers=headers)

print req3.content
like image 738
Leandro Campos Avatar asked Jan 31 '17 22:01

Leandro Campos


2 Answers

Use string without {}

data = '{"clienteLogin":"Token":"'+token+'","Operador":"","IdUnidadeNegocio":5,"PalavraCaptcha":"","Senha":"muranga21","cadastro":"on","Email":"[email protected]"},"mesclarCarrinho":true,"Token":"'+token+'","IdUnidadeNegocio":5,"Operador":""}'

or dictionary and requests will convert it into string.

data = {
    "clienteLogin": {
        "Token": token,
        "Operador": "",
        "IdUnidadeNegocio": 5,
        "PalavraCaptcha": "",
        "Senha": "muranga21",
        "cadastro": "on",
        "Email": "[email protected]"
    },
    "mesclarCarrinho": True,
    "Token": token,
    "IdUnidadeNegocio": 5,
    "Operador":"",
}

EDIT: you don't need json.dumps() and header - requests has json=

data = {
    "clienteLogin": {
        "Token": token,
        "Operador": "",
        "IdUnidadeNegocio": 5,
        "PalavraCaptcha": "",
        "Senha": "muranga21",
        "cadastro": "on",
        "Email": "[email protected]"
    },
    "mesclarCarrinho": True,
    "Token": token,
    "IdUnidadeNegocio": 5,
    "Operador":"",
}

url = "https://carrinho.extra.com.br/Api/checkout/Cliente.svc/Cliente/Login"
req3 = s.post(url, json=data)

print(req3.json())

Result:

{'EfetuarLoginResult': {'DataNascimentoAno': '1984', ... }

BTW: req3.json() converts to python dictionary

data = req3.json()

print(data['EfetuarLoginResult']['DataNascimentoAno'])
like image 111
furas Avatar answered Oct 09 '22 10:10

furas


You have enclosed your data variable in single quotes. Remove them and just use a standard dict.

like image 21
Daniel Roseman Avatar answered Oct 09 '22 09:10

Daniel Roseman