TypeError:pycurl python flask API Developmen中setopt的参数无效

2024-05-14 22:23:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python flask构建一个webserviceapi。当我执行以下代码时:

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

…很好用。但是我不能将STUDENTNUMBER传递给这个函数。在

我试过两种方法:

  1. Concat生成一个字符串并将其传递给c.setopt(c.URL,)这个函数

    a.方法1
    b、 方式2
    c、 方式3

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

TypeError: invalid arguments to setopt

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

    a.方式4

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

    Method not allowed. Please see the for constructing valid requests to the service

    所以我要转到这个链接:

    b.路5

    这样我得到了同样的错误

    TypeError: invalid arguments to setopt

方法1:

^{pr2}$

方式2:

^{3}$

方法3:

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

方式4:

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)

我该怎么做?在


Tags: 方法apilocalhosthttpurl错误service方式
1条回答
网友
1楼 · 发布于 2024-05-14 22:23:31

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

我建议使用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:

^{pr2}$

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

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

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

>>> print (r.text)
I am the 404's response body.

相关问题 更多 >

    热门问题