繁体   English   中英

如何在 Python 中使用这个 API? 我就是不能

[英]How to consume this API in Python? I just can't

So i'm trying to consume this API, I got this URLhttp://www.ventamovil.com.mx:9092/service.asmx?op=Check_Balance

在那里,您可以在输入字段上写下 {"User":"6144135400","Password":"Prueba$$"} 并得到响应。

https://i.stack.imgur.com/RTEii.png

回复

但是当我尝试在 python 上消费这个 api 时,我就是不能,我不知道如何正确消费:

我的代码

如您所见,我的代码得到了不同的响应,我应该得到与“响应”图像相同的响应。

为了节省一些时间,您可以使用他们的请求自动构建 python 代码,您所要做的就是:

  • 就像一开始一样,在输入字段中输入 json 并调用。
  • 打开网络选项卡,将他们发出的帖子请求复制为 curl
curl 'http://www.ventamovil.com.mx:9092/service.asmx/Check_Balance' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36' -H 'Origin: http://www.ventamovil.com.mx:9092' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Referer: http://www.ventamovil.com.mx:9092/service.asmx?op=Check_Balance' -H 'Accept-Language: en-US,en;q=0.9,ar;q=0.8,pt;q=0.7' --data 'jrquest=%7B%22User%22%3A6144135400%2C+%22Password%22%3A+%22Prueba%24%24%22%7D' --compressed --insecure
  • Go to postman and import the curl, then click code and select python, and here you go you have all the right headers needed
import requests

url = "http://www.ventamovil.com.mx:9092/service.asmx/Check_Balance"

payload = 'jrquest=%7B%22User%22%3A6144135400%2C+%22Password%22%3A+%22Prueba%24%24%22%7D'
headers = {
  'Upgrade-Insecure-Requests': '1',
  'Content-Type': 'application/x-www-form-urlencoded',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

如您所见,它们将输入作为表单编码的有效负载接受。

您需要修改此请求以在每次使用时使用您想要的用户/密码进行参数化。

顺便说一句,这个 python 代码的 output 是:

b'<?xml version="1.0" encoding="utf-8"?>\r\n<string xmlns="http://www.ventamovil.com.mx/ws/">{"Confirmation":"00","Saldo_Inicial":"10000","Compras":"9360","Ventas":"8416","Comision":"469","Balance":"10345.92"}</string>'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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