0

I am trying to build a Webservice API using python flask. When I execute this code below:

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

...it works fine. But I could not pass STUDENTNUMBER to this function.

I have tried two ways:

  1. Concat build a string and pass it to c.setopt(c.URL,) this function

    a. Way 1
    b. Way 2
    c. Way 3

Via those ways I got the same error:

TypeError: invalid arguments to setopt

  1. Pass the variable using c.setopt(c.POSTFIELDS, post_data)

    a. Way 4

    This way I got the same error:

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

    So that I am going to this link:

    b. Way 5

    This way I got the same error

    TypeError: invalid arguments to setopt

Way 1:

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

Way 2:

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

Way 3:

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

Way 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')

Way 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)

How can I make this work?

trincot
  • 317,000
  • 35
  • 244
  • 286
Rabiul
  • 1
  • 1

1 Answers1

0

This doesn't appear to be a question about Flask. It seems you're trying to write some code to query the API (which may be Flask powered).

I suggest using the Python requests library for this, as you can then define your parameters as a dictionary which is much easier. Not to be confused with Flask.request which is a different thing entirely!

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

The last line above sends the request, and you can see the full URL with:

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

Or print the response code, which is a 404 in my case:

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

If the response contains data you can access this through r.text:

>>> print (r.text)
I am the 404's response body.
v25
  • 7,096
  • 2
  • 20
  • 36