Python String format for URLs - python-3.x

I am trying to post a payload to a rest endpoint using the python requests library and having an issue with the formatting of the endpoint. The below code works:
endpoint="https://api.something/path/to/my/endpoint"
headers= {"Authorization": "Bearer <token>"}
req = requests.post(endpoint, headers=headers, data).json()
However I were to change the endpoint to something like this
my_var="my"
endpoint="https://api.something/path/to/{}/endpoint".format(my_var)
My post request no longer works giving me a not found . In both cases the endpoint resolves to the exact same URL but yet using format doesnt seem to work.
What am I missing ?

Related

valid json response with Postman but Invalid response with https module

I am calling this endpoint
https://www.linkedin.com/voyager/api/typeahead/hitsV2?keywords="sousse tunisia"&origin=OTHER&q=type&queryContext=List(geoVersion-%3E3,bingGeoSubTypeFilters-%3EMARKET_AREA%7CCOUNTRY_REGION%7CADMIN_DIVISION_1%7CCITY)&type=GEO
With Postman I get a valid json response. but when I call the same endpoint using https module in node.js code, I get an invalid json response with status 200.
the invalid response is something like this �������Q�[#��=��M�x��Ik�CzEa:H
I am using the accept header like this: _headers['accept']= 'application/vnd.linkedin.normalized+json+2.1'
the other endpoint used a gzip encryption. but this endpoint doesn't.
so I just set the accept-encoding to empty _headers["accept-encoding"] = "". It was confusing that even with the wrong encoding postman showed a correct response.

Getting gibberish when trying to download file from sharepoint using microsoft graph api

I'm trying to download some excel file from our sharepoint. I'm following instructions from this resource: https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http
when I try to call the endpoint from Postman, I get some gibberish and a pretty long one. the tutorial says I'm supposed to get a 302 response but I get a 200 response.
I also have all the permission granted for read write for Site and Files.
from postman, I translate the code to python. here's my code. I get the same result on my python script:
url = "https://graph.microsoft.com/v1.0/drives/<our_drive_id>/items/<target_file_id>/content"
payload={}
headers = {
'Content-Type': 'application/json',
'SdkVersion': 'postman-graph/v1.0',
'Authorization': f'Bearer {token}' # retrieved from an endpoint called prior
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response)
here is the response that I get:
I'm not really sure since I don't get an actionable error. Also the response that I get is different from the expected response stated in the tutorial.
The documentation says that the request returns a 302 Found response redirecting to a pre-authenticated download URL for the file.
Postman follows the Location header in the response and makes a redirect to URL and downloads the content.
If you disable Automatically follow redirects in Postman settings then the response will be 302 and Location header will contain a download link.

Twitter Search API: Getting error 32 could not authenticate you with specific searches but not every search

I'm using python3 to send a request to the Twitter Search API. I'm testing these requests in Postman, and then using the code snippit generator to copy and paste into my editor to run like so:
url = "https://api.twitter.com/1.1/search/tweets.json?q=twitter"
payload={}
headers = {
'Authorization': 'OAuth oauth_consumer_key="",oauth_token="",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1613517432",oauth_nonce="x",oauth_version="1.0",oauth_body_hash="",oauth_signature=""',
'Cookie': 'personalization_id=""; lang=en; guest_id='
}
twitter_response = requests.request("GET", url, headers=headers, data=payload)
The requests work just fine in Postman. But in my script, the request only works when the query parameter for the URL is "nasa". If I change it to anything else, I get error 32 Could not authenticate.
Is there some quirk about Python I don't know about when it comes to the Twitter Search API? Any guidance would be helpful here.
Sounds like you’re copying and pasting the OAuth headers from the Twitter Dev docs instead of calculating them with every new query. I think the docs use NASA as an example search query.
I was able to find a way around this by using the Tweepy API wrapper, which solved my OAuth issues. The headers weren't getting formatted properly for the request.

Not getting auth headers when setting axios default

I am trying to send an auth header along with an axios POST request from inside a Vue application. I currently am getting a 401 from my back end with an auth header that works when I do a curl.
I've tried splitting it up into variables and putting it in but that did not work and resulted in the same error (401).
This is just the axios code I am trying to get to work. I have checked with console.log and all values I am trying to send exist, though I don't know how to check the axios headers before sending.
axios.defaults.headers.common["Authorization"] = JWTtoken;
axios.post(updateURL, {
token: result.token
});
The backend code can't be changed easily for testing so need to figure out why not sending from the front end
I'd like it to send the correct header along with my request so I don't get a 401 status code.
I think you need this..
axios.defaults.headers.common["Authorization"] = "Bearer " + JWTtoken;
axios.post(updateURL, {
token: result.token
});
Notice that I add Bearer in the Authorization. It is how JWT was meant to be used according to their introduction.
However, if the answer is wrong. Help us by providing more information about your response in Developer Console as #RuChernChong suggest. Any error logs would be helpful as well.
Another way by using Axios globals to set for example X-Auth-Token encoding from JWT.io directly like this:
axios.defaults.headers.common["X-Auth-Token"] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";

API get request not returning correct data in python

I am trying to get data from Uni Stats API by sending a get request. Following is my code:
import requests
dt = requests.get(url='https://API_TOKEN#data.unistats.ac.uk/api/v4/KIS/Institution/10007856.json')
print(dt)
Whenever I run this code it returns me <401 Response> but If I try the same url https://API_TOKEN#data.unistats.ac.uk/api/v4/KIS/Institution/10007856.json in the Postman software it returns correct data in json format.
So I wanted to know how can I send a get request with requests which can return correct data?
requests.get('https://data.unistats.ac.uk/api/v4/KIS/Institution/10007856.json', auth=('user', 'pass'))
401 is an authorization error, provide authorisation to the request.get method.
Depending on your user agent, you may be able to include the access token in the url as in the following examples:
https://accesstoken#data.unistats.ac.uk/api/v4/KIS/Institution/{ukprn}/Course/{kisCourseId}
Emphasis added.

Resources