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
datacan be string or python dictionary and requests converts it into string. You createset()because{}is also used to createset()if you doesn't use keys and values inside.