使用API​​ python发送POST请求

时间:2014-09-17 06:46:46

标签: python api

我在post post请求下执行并收到错误"文件过早结束"

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import os, unittest, time, re, random, datetime
import requests


class Sauce(unittest.TestCase):

def test_create_contract(self):
    payload = {'PO_ID': '3557698',' MAN': 'HQF01'}
    r = requests.post(
        "https://time.tps.com/xml/Msg.jsp?msg=MSG_RAM_INFO&user=Dist1&pass=ome1")
    print r
print r.text


if __name__ == "__main__":
    unittest.main()

当我尝试使用XML文件的soap客户端时,其工作正常 我的XML文件是

<SO>
  <PO_ID>3557698</PO_ID>
  <MAN>HQF01</MAN>
</SO>

我需要发送一个帖子请求,需要得到回复。

1 个答案:

答案 0 :(得分:0)

r=requests.post("https://time.tps.com/xml/Msg.jsp?msg=MSG_RAM_INFO&user=Dist1&pass=ome1")

但你真的没有发送帖子数据。您应该将data = payload添加到该帖子请求中。

url = "https://time.tps.com/xml/Msg.jsp?msg=MSG_RAM_INFO&user=Dist1&pass=ome1"
payload={'PO_ID': '3557698','MAN': 'HQF01'}
r=requests.post(url,data = payload)

编辑:

试试这个

payload = "<SO><PO_ID>3557698</PO_ID><MAN>HQF01</MAN></SO>"
headers = { 'Content-Type': 'application/xml' }
r=requests.post(url, data = payload, headers = headers)