I am able to make Post request using postMan and I get correct data but when I send the same using python ,I get a different output .
As you can see below using postMan when I make a request I can see the desired output .
But when I do the same using my below python code ,it doesn't give me the desired output and overall just gives me the a HTML text with fields filled up ,seems it makes a Get request ,not sure If am sending my form incorrectly
def checkDate(self):
values ={'numberItems':1,
'mode':'DriveTest',
'officeId':592,
'requestedTask':'DT',
'firstName':'xxx',
'lastName':'xxx',
'dlNumber':'xxx',
'birthMonth':05,
'birthDay':31,
'birthYear':123,
'resetCheckFields':'true'}
r = requests.post('https://www.dmv.ca.gov/wasapp/foa/findDriveTest.do',params=values)
tet=r.text
print(tet)
The actual page is https://www.dmv.ca.gov/wasapp/foa/findDriveTest.do ,so here what I am trying to do is create a script which will run every 4 hours to notify me that a early date is now available .
To make a POST request you should pass your values like this:
r = request.post('https://www.dmv.ca.gov/wasapp/foa/findDriveTest.do',data=values)
The reason that your previous code didn't make a POST request is that you were trying to pass your values as URL parameters which is used when making a GET request
Try using this :
import json
r = requests.post('https://www.dmv.ca.gov/wasapp/foa/findDriveTest.do',params=json.dumps(values), headers = {"content-type": "application/json"})
tet=r.text
print(tet)
Related
I am trying to use the request with the post method. And one of my parameters has "/"(slash) so the response turns into an error. When I look at the post URL then it seems "/" was written as "%2F". The code and results are like following:
link="https://api.someexchange.com"
sub_url="/open/v1/orders"
stamp = str(int(time.time())*1000)
headers={}
headers["Content-Type"]="application/x-www-form-urlencoded"
parameters={"symbol":"BTC/TRY",
"side":1,
"type":1,
"quantity":0.001,
"price":321000,
"timestamp":stamp,
"api_key":"api_key"}
r=requests.post(link+sub_url,params=parameters,headers=headers)
print(r.url)
'https://api.someexchange.com/open/v1/orders?symbol=BTC%2FTRY&side=1&type=1&quantity=0.001&price=321000×tamp=1669994305000&api_key=api_key'
as you can see, the URL has "BTC%2FTRY" instead of "BTC/TRY" when I try manually from the URL bar, It works fine.
I was trying to implement an API using Python, and flask to help myself learn and practice REST.
The idea was to receive a HTTP POST with data that looks like as such:
{"startDate":"2015-07-01","endDate":2015-07-08","within":{"value":9000,"units":miles}} and send some of the data to a NASA API(endpoint).
I was able to create a POST method , and I am able to receive the data (both in POSTMAN and in the browser). Here is the relevant code :
#neows.route('/UserInput',methods=['GET','POST'])
def UserInput():
startDate = request.args.get('startDate')
endDate = request.args.get('endDate')
#print (type(startDate))
#print (type(endDate))
getAsteroids(startDate,endDate)
return jsonify(request.args)
But when I extract some data from the POST above to send to a NASA API (GET), I am receiving this error:
werkzeug.exceptions.BadRequestKeyError
Here is the url I am trying to hit : (https://api.nasa.gov/neo/rest/v1/feed?start_date=START_DATE&end_date=END_DATE&api_key=API_KEY)
I am able to hit the url both on POSTMAN and browser, outside of my code.
The relevant piece of code with the error is posted below and the line that seems to be throwing the error is in Italics (marked with *).
def getAsteroids(startDate,endDate):
API_KEY='xxx'
print (startDate)
print (endDate)
*result=request.args["https://api.nasa.gov/neo/rest/v1/feed?
start_date="+startDate+"&end_date="+endDate+"&api_key="+API_KEY+""]*
I would really appreciate if some one could help me understand and resolve this issue.
If you want to do a request against the NASA's API you can use requests module. (Or any other module to send HTTP requests)
import requests
# ...
def getAsteroids(startDate, endDate):
API_KEY='xxx'
payload = {'start_date': startDate, 'end_date': endDate, 'api_key': API_KEY}
result = requests.get('https://api.nasa.gov/neo/rest/v1/feed', params=payload)
request.args is something different used to get the parameters of the incoming request.
I am attempting to use a facial recognition API and am still new to the requests package. The code I have written is posted below.
import requests
baseURL = "https://api-live.wiseai.tech"
appKey = "--------"
appSecret = "----------"
def createFaceDB(appKey, appSecret, libraryName, thresholds):
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
body = {
"appKey": appKey,
"appSecret": appSecret,
"libraryName": libraryName,
"thresholds": thresholds
}
r = requests.post("http://api-live.wiseai.tech", data= body, headers=headers)
return r.text
print(createFaceDB(appKey, appSecret, "test", 1))
The documentation for the API states that there is either going to be a successful requests or an unsuccessful one. If either occurs I am suppose to get either success message or an error message respectively. The error messages vary such as ERROR_KEY_ISNOT_LEGAL indicating there is something incorrect with the API key or BAD_REQUEST indicating there are missing parameters.
Unfortunately, when I run the code I get back a bunch of gibberish on the command prompt. Not receiving a successful or unsuccessful request. Furthermore, if I incorrectly put in the API key expecting to receive a error message I get the same output in the console.
Both API and appSecret keys are correct and available. Unfortunately, at this moment I am unable to share them.
I've added more information the question along with images. Linked below.
https://imgur.com/a/wy11JUf
Edit 1: Some of the other things I've tried is setting json=body. Another thing is that the output display grecaptcha at the end of it (as displayed in the image). Just wanted to point that out, not sure exactly what its suppose to mean.
Edit 2: It seems that even though the body consists of 4 values and the definition expects 4 parameters if I remove appKey and appSecret I get the same results on the console. Perhaps there is another command I could use instead of requests.post()
I'm trying to write API client for Jira with Python requests lib according reference:
https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/
Request to be generated:
http://localhost:8080/rest/api/2/search?jql=assignee=charlie&startAt=2&maxResults=2
As I know, parameters to GET request should be passed as dictionary like:
params = {'assignee':'charlie', 'startAt':'2'}
But all main parameters are nested in jql parameter, so I assume there is should be a nested dict like:
params = {'jql': {'assignee': 'charlie'}}
But that's doesn't work - as a result I've got request to
/rest/api/2/search?jql=assignee
As expect /rest/api/2/search?jql=assignee=charlie
using
r = requests.get(url, params=params)
How to manage such request?
UPD:
To be more clear, I'd like to wrap request in a method with kwargs, like:
search_query(assignee='charlie', startAt=1, etc...)
And then generate a query using this params, but maybe there are any other ideas.
You are missing couple of key parameters, mainly if you are pushing data via requests, the data go into the data argument. Also the moment you push JSON data, you need to set the headers correctly as well. The last thing is authentication. Have you tried to post it in this manner?
import json
requests.post(url=url, headers={"Content-Type": "application/json"},
auth=('username', 'password'), # your username and password
data=json.dumps(params)
)
Also by the JIRA documentation you've provided (https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/) if you want to push query as data, the url you want is /rest/api/2/search.
I'm trying to connect our application to Streamlabs' API so we can post donation alerts. To do that, I need to get an access token for the user whose channel we want to alert on. It's a normal OAuth2 thing. We hit the /authorize endpoint and get a code back, which we're then supposed to able to use to get an access token from the /token endpoint (https://dev.streamlabs.com/v1.0/reference#token-1).
But when we send the POST request for the token, we get an error saying the request is missing the "grant_type" parameter.
We're using the normal requests library. I've tried changing the format of the request to requests.post. I've tried altering the data by wrapping it in urlencode and json.dumps. Still no luck.
streamlabs_client_id = config('STREAMLABS_CLIENT_ID')
streamlabs_client_secret = config('STREAMLABS_CLIENT_SECRET')
streamlabs_redirect_uri = config('STREAMLABS_REDIRECT_URI')
grant_type = 'authorization_code'
querydict = {
"grant_type":"authorization_code",
"client_id":streamlabs_client_id,
"client_secret":streamlabs_client_secret,
"redirect_uri":streamlabs_redirect_uri,
"code":code
}
url = "https://streamlabs.com/api/v1.0/token"
streamlabs_response = requests.request("POST", url, data=querydict)
This is the json I get back every time:
{"error":"invalid_request","error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the \"grant_type\" parameter."}
Any ideas what I'm doing wrong with the data?