0

我正在尝试使用 python 烧瓶构建 Webservice API。当我在下面执行此代码时:

http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&STUDENTNUMBER='1234'&SESSIONKEY=94194202323

...它工作正常。但我无法传递STUDENTNUMBER给这个函数。

我尝试了两种方法:

  1. Concat 构建一个字符串并将其传递给c.setopt(c.URL,)此函数

    一个。方式 1
    b。方式 2
    c。方式3

通过这些方式,我得到了同样的错误:

TypeError:无效的参数setopt

  1. 使用 c.setopt(c.POSTFIELDS, post_data) 传递变量

    一个。方式4

    这样我得到了同样的错误:

    不允许的方法。请参阅构建对服务的有效请求

    所以我要去这个链接:

    湾。方式 5

    这样我得到了同样的错误

    TypeError:无效的参数setopt

方式一:

student_url = " http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&STUDENTNUMBER=%s&SESSIONKEY=94194202323"%student_number;
 c.setopt(c.URL,student_url)

方式二:

c.setopt(c.URL,"http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&STUDENTNUMBER=%s&SESSIONKEY=94194202323"%(student_number))

方式3:

c.setopt(c.URL,"http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&STUDENTNUMBER=%s&SESSIONKEY=94194202323"%student_number)

方式四:

c.setopt(c.URL,"http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&STUDENTNUMBER=%s&SESSIONKEY=94194202323")
c.setopt(c.POSTFIELDS, 'STUDENTNUMBER = 1234')

方式5:

post_data ={'STUDENTNUMBER' : '1234'}
c.setopt(c.URL,"http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&SESSIONKEY=94194202323")
c.setopt(c.POSTFIELDS, post_data)
c.setopt(pycurl.POST, 1)

我怎样才能使这项工作?

4

1 回答 1

0

这似乎不是关于 Flask 的问题。您似乎正在尝试编写一些代码来查询 API(可能由 Flask 驱动)。

我建议为此使用Python requests库,因为您可以将参数定义为更容易的字典。 不要混淆Flask.request这是完全不同的事情!

import requests
url = 'http://localhost/Service/API/Services.svc/XMLService/Students'
params = {'SEARCHBY': 'StudentNo', 
          'STUDENTNUMBER': '1234',
          'SESSIONKEY': 94194202323}
r = requests.get(url, params)

上面的最后一行发送了请求,您可以看到完整的 URL:

>>> print (r.url)
http://localhost/Service/API/Services.svc/XMLService/Students?SEARCHBY=StudentNo&STUDENTNUMBER=1234&SESSIONKEY=94194202323

或者打印响应代码,在我的例子中是 404:

>>> print(r)
<Response [404]>

如果响应包含数据,您可以通过以下方式访问r.text

>>> print (r.text)
I am the 404's response body.
于 2019-01-19T16:22:58.070 回答