I'm having a problem with a Python request, where I pass the body url to the API data.
Note: I have a Node.js project with TypeScript that works normally, prints to the screen and returns values. However if I try to make a request in Python it doesn't work with an error 401.
Below is an example of how the request is made in Python. can you help me?
import requests
url = 'https://admins.exemple'
bodyData = {
'login': 'admins',
'pass': 'admin',
'id': '26' }
headers = {'Content-Type': 'application/json'}
resp = requests.post(url, headers=headers, data=bodyData)
data = resp.status_code
print(data)
Please dump a dict to a json string as follows:
import json
resp = requests.post(url, headers=headers, data=json.dumps(bodyData))
You also can pass your dict to json kwarg
resp = requests.post(url, headers=headers, json=bodyData)
It will set Content-Type: application/json and dump dict to json automatically
You are not correctly authenticating with the server. Usually, you need to send the username and password to a "sign in" route which will then return a token. Then you pass the token with other requests to get authorization. Since I don't know any details about your server and API, I can't provide any more details to help you out.
Related
I've been trying to use an api on a website for awhile now and the responses from the api return the data in xml. I want the response to be in JSON format, but when I try adding a header into the http request, it keeps sending the response in xml.
I've tried the following code:
import requests
param_list = {'key1': 'value1'}
headers = {'Content-Type': 'application/json'}
url = 'api url'
response = requests.get(url=url, params=param_list, headers=headers,)
print(response.text)
print(response.headers)
The second print statement shows that the 'Content-Type' header returns "text/html"
Any idea on how to fix this? Thanks for your time and help!
I am trying to make an http post request in Python. The http that I am posting to requires an authorization. For security, I have replaced the real url, data and authorization. When I run the code below with the correct url, data body and authorization I get status 401. That would imply that the authorization is not correct. Is there another way I should be adding authorization, headers and body to my http post request so that the post is successful? The authorization password is correct and works in Postman, so it's contents is not the issue--I'm wondering if the format is. Thanks in advance!
import requests
from requests.auth import HTTPBasicAuth
def post():
url = 'http://somelongurl'
headers = {'Content-Type': 'application/json'}
auth = HTTPBasicAuth('Authorization', 'PASSWORD')
data = {"ProductID" : "ABCDEFG",
"ClientID": "10000",
"SerialNumber": "1234567890"}
req = requests.post(url, headers=headers, data=data, auth=auth)
return req
This was resolved by including the authorization in the headers and changing the data as type json as follows: https://postimg.cc/3y7B6fvL
Using a POST request, my goal is to receive an authorization code from a JSON response. However, the only response I'm getting is the HTML of the webpage I'm connecting to, not the desired JSON response.
import requests
from base64 import b64encode
appAuth = b64encode(b"QT8txxxxxxxx:n76mxxxxxxxxx").decode("ascii")
headers = { 'Authorization' : 'Basic %s' % appAuth }
url = "http://demo.skubana.com?grant_type=authorization_code&redirect_uri=demo.skubana.com/appstore&code=LCqYHU&cid=Y29tcGFueUlkMTI0MDYw"
r = requests.post(url, headers=headers,json={})
print(r.status_code)
print(r.content)
The supplied API documentation for Skubana is, in my opinion, shockingly under-specified.
What you may have missed is this part:
Add the request path: /oauth/token
which, to be fair, is not reflected in their 'sample URL':
Sample Url: https://blah.skubana.com?grant_type=authorization_code&redirect_uri=redirect.com/callback&code=ABCDE&cid=ABCD1234
When you change your URL to include that path, I get a 401 Unauthorized response instead:
>>> url = "http://demo.skubana.com/oauth/token?grant_type=authorization_code&redirect_uri=demo.skubana.com/appstore&code=LCqYHU&cid=Y29tcGFueUlkMTI0MDYw"
>>> r = requests.post(url, headers=headers)
>>> r.status_code
401
This too, incidentally, also involves a redirect from POST to GET. But I strongly suspect that with the right credentials (which I don't have) you'd actually get your token.
When you post to https://demo.skubana.com, you are redirected, first to the same URL to make a GET request, with:
Location: https://demo.skubana.com/?grant_type=authorization_code&redirect_uri=demo.skubana.com/appstore&code=LCqYHU&cid=Y29tcGFueUlkMTI0MDYw
then on to your dashboard:
Location: /work/dashboard
and finally to the login page:
Location: https://demo.skubana.com/login
All this is recorded in the r.history list:
>>> r.history
[<Response [302]>, <Response [302]>, <Response [302]>]
Note that you don't have to set json={}, just use:
r = requests.post(url, headers=headers)
The requests library can take care of the Base64 encoding for you as that's just part of the Basic Authentication scheme, just pass in the YOUR_APP_KEY and YOUR_APP_SECRET values as username and password:
YOUR_APP_KEY = "QT8txxxxxxxx"
YOUR_APP_SECRET = "n76mxxxxxxxxx"
url = "http://demo.skubana.com/oauth/token"
parameters = {
"grant_type": "authorization_code",
"redirect_uri": "demo.skubana.com/appstore",
"code": "LCqYHU",
"cid": "Y29tcGFueUlkMTI0MDYw",
}
r = requests.post(url, params=parameters, auth=(YOUR_APP_KEY, YOUR_APP_SECRET))
Finally, given the redirect and complete lack of POST parameters, I strongly suspect that a GET request works just as well:
r = requests.get(url, params=parameters, auth=(YOUR_APP_KEY, YOUR_APP_SECRET))
This just bypasses the POST -> GET redirect.
Normally, a OAuth2 request for a token like this would post the parameters in the POST body, so do also try:
r = requests.post(url, data=parameters, auth=(YOUR_APP_KEY, YOUR_APP_SECRET))
if the params=parameters options don't get you your token, still.
So i'm writing a program that post's data to a url and get's the response. In postman it requires a token. So when I tried to make it in python it's giving me a response [401].
The problem I have is trying to get the token first and then passing it to my post_data method.
I'm going to put *** by the URL and username and password for privacy concerns.
import requests
import json
import pprint
import urllib
def get_token():
tokenurl='***'
data={
'grant_type':'password',
'username':'***',
'password':'***'
}
token=requests.post(tokenurl,data=data)
print(token)
get_token()
def post_data():
url1='***'
data={"***"
}
data_json = json.dumps(data)
headers = {'Content-type': 'application/json'}
response = requests.post(url, data=data_json, headers=headers)
pprint.pprint(response.json())
In the post_data() function, you can add your generated token to the headers
headers = {'Content-type': 'application/json','Authorization': 'token ***'}
*** is your generated token
I am trying to learn more about using http requests and the Mailchimp API, but I cannot seem to figure out how to add a member to list using a post request. I have tried multiple configurations, and I guess I made some headway since my response went from a 405, to a 401, now I'm getting a 400. I assume this means that I am being authenticated, but I am formatting the request incorrectly.
I have gotten it to work the python mailchimp library, but I want to actually learn how to use the HTTP requests. I could find very few examples of using python requests with Mailchimp.
(obviously I put in my actual list_id, my_username, and my_apikey)
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'blah#blah.com',
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
payload = json.dumps(data1)
response = requests.post(members_url, auth=auth, headers=headers, json=payload)
This is my response:
>>> response
<Response [400]>
I'm stumped....what am I doing wrong?
This worked:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah#blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
This worked:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah#blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
My problems were #1 I was double encoding the json data and #2 mailchimp recognizes obviously BS email addresses like blah#blah.com
Thanks #Alasdair