8

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
2
  • 1
    data can be string or python dictionary and requests converts it into string. You create set() because {} is also used to create set() if you doesn't use keys and values inside. Commented Jan 31, 2017 at 22:28
  • I'm new on python what i need to do to fix? Commented Jan 31, 2017 at 22:34

2 Answers 2

8

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

requests has json= Thank you!!! I was messing around with all these old recommendations and stumbed on your answer. That info was key. 💪
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.