How to change request headers - python

How do I set request headers? I am downloading image from instagram and I want to change its filename, get the file size. These many request headers are there wiki
This is what I have done until now
import requests
#app.route('/try')
def trial():
img = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
imgData = requests.get(img)
return imgData # this gives me error: Response object is not callable
Edit : I want to set the content-disposition header . Another question : when I set the headers, then how would I give the image to the client side?
I read on Internet that to send the file to client side I should set the request header. But how do I send the file is my another question? Sorry for asking another question in the same

The query which you asked has nothing to do with Flask. Flask is a web framework. Requests is a HTTP library which will help solve your issue
You just need to create a dict with your headers (key: value pairs where the key is the name of the header and the value is, well, the value of the pair) and pass that dict to the headers parameter on the .get or .post method.
headers = {'Content-Type': 'text/plain'}
r = requests.get('http://example.com', headers=headers)
If you wish to check the header values set when the request was sent then you can simple do this:
print r.headers
print r.headers['Content-Type'] # Output - 'text/html'

resp = requests.get('https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-15/e35/12093691_1082288621781484_1524190206_n.jpg')
resp.headers
And you will get the response like this ..
{'content-length': '73921', 'last-modified': 'Fri, 30 Oct 2015 15:18:29 GMT', 'connection': 'keep-alive', 'cache-control': 'max-age=1209600', 'date': 'Wed, 18 Nov 2015 08:44:15 GMT', 'access-control-allow-origin': '*', 'content-type': 'image/jpeg', 'timing-allow-origin': '*'}
You can get headers... check out the content-length. And you will not gonna get the file name I believe because content-disposition will not be there. So try something else to get the file name.

Related

Windows PowerShell & InfluxDB: Unable to Write Data to Bucket

I am new to InfluxDB. Understand we need to use Windows Powershell to interact with InfluxDB.
Using Python in the shell, I tried to write data to the bucket using the below code
import influxdb_client, os, time
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
token = os.environ.get("INFLUXDB_TOKEN")
org = "nil"
url = "https://us-west-2-1.aws.cloud2.influxdata.com/"
client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
bucket="MyBucket"
write_api = client.write_api(write_options=SYNCHRONOUS)
for value in range(5):
point = (
Point("measurement1")
.tag("tagname1", "tagvalue1")
.field("field1", value)
)
write_api.write(bucket=bucket, org="nil", record=point)
time.sleep(1) # separate points by 1 second
But I get the error
influxdb_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Thu, 29 Dec 2022 01:44:17 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '55', 'Connection': 'keep-alive', 'trace-id': '5374d7ae5df282f4', 'trace-sampled': 'false', 'x-platform-error-code': 'unauthorized', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains', 'X-Influxdb-Request-ID': '2e52a39e6d7442b5fc7eb7306ef004d4', 'X-Influxdb-Build': 'Cloud'})
HTTP response body: {"code":"unauthorized","message":"unauthorized access"}
401 indicates that there should something wrong with the authorization header. Could you try to enable to debug log as follows to see the details:
client = influxdb_client.InfluxDBClient(url=url, token=token, org=org, debug=True) // see that **debug=True** will enable verbose logging of HTTP requests
Both HTTP request headers and body will be logged to standard output. And please check the HTTP header to see whether there is "Authorization: Token" header, which is similar to
Authorization: 7mexfXXXXXXXXXXXXX
Please double check if the header is missing or malformed.

How to access numeric data from API call?

I am accessing data on futures prices from the Chicago Mercantile Exchange via API calls.
I am using an URL which, when I post it in a browser, returns a csv-file (gzip compressed). I do however need to write a program in python that can take in several thousand URLs and handle the data it receives. When using this URL in a requests.get() command in python, I get a response which I do not know how to read.
import requests
url='https://datamine.cmegroup.com/cme/api/v1/download?fid=20181211-
EOD_xcbt_ff_fut_0-eth_p'
user = '***'
password = '***'
r = requests.get(url, auth=(user, password))
print(r.headers)
Which, among other things, returns:
{
'Date': 'Sun, 30 Dec 2018 13:01:07 GMT',
'Content-Type': 'application/x-gzip',
'Content-Length': '1287',
'Content-disposition': 'attachment; filename="xcbt-eodp-ff-fut-20181211.csv.gz"'
}
I assume that the prices I am trying to obtain are contained in the element 'attachment; filename="xcbt-eodp-ff-fut-20181211.csv.gz"'.
My question:
How do I obtain the prices from the response I am getting?
Then, how do I import the data into a DataFrame?
Simply using:
print(r.content)
does not return numeric values.
You should do something like:
resp = requests.get(url)
# for now it's a Response object. Make a json out of it
json_resp = resp.json()
print(json_resp.get('some_key')) # or what ever you need
Then use a simple dict functionality to extract what ever you need..

python requests post file, post a file and data at the same time

I am very new to python (and coding in general) so I hope I articulate my issues well enough. I've managed to cobble together an application that connects to the API of one of my companies software.
I've been able to auth, get the token, pass it to other functions to GET and POST to some API functions. I'm working on a new API call that posts a file. I am STUMPED. All the examples I find show only passing the file but I need to pass the file, and data, and an auth header. I have tried so many variations of my code, and nothing has gotten me any closer.
First, this function is what works, but with a different API (groups) for posting a new group. It doesn't include any files.
def apiPost(token):
if not token == "":
status.run_set_text('Running API POST Call', 3)
headers = { 'Content-Type':'application/json', 'Authorization':'Bearer '+str(token) }
data = {"data":{ 'id':0, 'customerId':33, 'name':'567tuhj', 'description':'sdfgsdfg'}}
r = requests.post(api_url+"v1.1/groups/", headers=headers, data=json.dumps(data))
***other code removed updating status bars***
return r
else:
***other code removed updating status bars***
My dev environment only has access to one customer, but it still requires that I post the customerId to POST.
I've tried hundreds of variations of converting this to posting a file from what I read on the requests site tutorials and some other stackoverflow questions. This posts to the API packageFiles. According to the Swagger page I have access to, it says I need to include the ID and customer ID when uploading the file still.
def apiPost(token):
if not token == "":
status.run_set_text('Running API POST Call', 3)
headers = {'Authorization':'Bearer '+str(token)}
files = {'file': open('log.txt', 'rb')}
data = {"data":{ 'id':0, 'customerId':33}}
r = requests.post(api_url+"v1.1/package_files/"+set_api_pair_value, headers=headers, data=json.dumps(data), file=files)
***other code removed updating status bars***
return r
else:
***other code removed updating status bars***
First, you should format your code to be clean, nice and readable
I am trying to solve your issue, but I think you should attach to your question some kind of expected data, file, request parameters.
def apiPost(token):
if not token == "":
status.run_set_text('Running API POST Call', 3)
headers = { 'Content-Type':'application/json',
'Authorization':'Bearer '+str(token) }
data = {"data":
{ 'id':0,
'customerId':33,
'name':'567tuhj',
'description':'sdfgsdfg'
}
}
#The part you are looking for probably
files = {'name_of_file_field' : open(filename, "rb")}
r = requests.post(api_url+"v1.1/groups/", headers=headers, data=json.dumps(data), files = files)
***other code removed updating status bars***
return r
else:
***other code removed updating status bars***
Comment:I get back a 400 (bad request) with that accepted answer..
{"status":"FAILURE",
"error":[{"code":"5004",
"name":"General Error","severity":"3","message":"Error Occurred During the operation",
"details":{"5004":"General Error null"}}]}
{'Connection': 'keep-alive', 'X-Powered-By': 'Undertow/1', 'Server': 'WildFly/9', 'Content-Length': '172', 'Content-Type': 'application/json;charset=UTF-8', 'X-Application-Context': 'application:9090', 'Date': 'Fri, 28 Sep 2018 17:57:57 GMT'}
Please edit your Question and add your Python and requests Version!
Tried the following, using a Text file instead of Image:
import requests
url = 'http://httpbin.org/anything'
files = {'file': ('helloworld.txt', open('../test/helloworld.txt', 'rb'), 'text/text')}
data = dict(name='barca', country='spain')
r = requests.post(url, files=files, data=data)
# Print my requests.post header, files and data.
r_dict = r.json()
for key in r_dict:
print('{}:{}'.format(key, r_dict[key]))
Response from http://httpbin.org/anything:
<Response [200]>
My requests.post header, files and data, send back from host.
method:POST
files:{'file': 'Hello World'}
url:http://httpbin.org/anything
form:{'name': 'barca', 'country': 'spain'}
origin:xx.xx.xx.xx
args:{}
headers:{'Content-Length': '369',
'Accept': '*/*',
'Content-Type': 'multipart/form-data; boundary=bc3c1927f542430f8166e8f3f27f3c72',
'Host': 'httpbin.org', 'Connection': 'close',
'User-Agent': 'python-requests/2.11.1',
'Accept-Encoding': 'gzip, deflate'}
json:None
data:
Tested with Python:3.4.2 - requests:2.11.1

HTTP request with requests in python returning text instead of json

def thready(name,count):
payload={'Accept': 'application/json;charset=utf-8', 'X-Mashape-Key': 'key'
}
link = "https://montanaflynn-gender-guesser.p.mashape.com/?name=" + name
r=requests.get(link ,headers=payload)
print r.headers
data=r.json()
print data
count=0
thready("bob",count)
So I just tried to do a simple HTTP request in python. In the r.header i get
{'date': 'Wed, 22 Jul 2015 06:30:12 GMT', 'content-length': '178', 'content-type
': 'text/html', 'connection': 'keep-alive', 'server': 'Mashape/5.0.6'}
In the header I said it should return json. The return is text though? Very confused about this fact? Some insight would be very helpful because I'm trying to do the simple
There is nothing wrong with the code.
I have inspected your API request upstream by going to the source: the API provider's page and it looks like the API is defunct/not working as expected.
Which is exactly why you're getting errors.
Solution:
1) Contact the provider to try to resolve the problem, its on them not on you to fix it
2) Find an alternative API from the same portal: https://www.mashape.com/explore?query=gender
Good luck

LinkedIn out of network API calls returning "unauthorized" error

I'm trying to access out-of-network profiles using the LinkedIn API, following the instructions from here.
I'm writing my code in Python.
As I understand it, I just add an extra header to my HTTPSConnection request:
normal call:
connection.request(method, relative_url, body = body, headers={'Authorization': OAuth_header})
out-of-network call:
// these values were extracted from the http-header I received
name = x-li-auth-token
value = name:R8Y4
connection.request(method, relative_url, body = body, headers={'Authorization': OAuth_header, name: value})
When I do the out-of-network call, I get an error:
error:
status: 401
timestamp: 1330027911625
request-id: VHUSL0J7TL
error-code: 0
message: [unauthorized]. OAU:k1tofeoqr4id|2dc38f4e-73d1-4d31-9330-dd82aca89616|*01|*01:1330027911:CRc7YYYQRe2VS6woJpGX+qYVa/Q=
I've been testing this on both my own profile and an actual out-of-network profile, no change in error.
From various API requests, the "value" changes slightly, and I have tried all variants:
"name:R8Y4"
"search-name:R8Y4"
"OUT_OF_NETWORK:R8Y4"
I'm guessing it has to do with the HTTP headers, but I have no idea what is wrong.
Please help! Thank you.
I'm not sure why your call is failing. Here's the sequence using the tested oauth2 library in python, including the entire HTTP conversation.
First, do the search:
http://api.linkedin.com/v1/people-search:(people:(distance,id,first-name,last-name,headline,api-standard-profile-request))
Parameters (I'm using OAuth in the parameters for this call)
oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D
oauth_nonce=41358038
oauth_timestamp=1330098205
oauth_consumer_key=xxx
oauth_signature_method=HMAC-SHA1
facet=network%2CO
oauth_version=1.0
oauth_token=xxx
keywords=Schneider+Electric
oauth_signature=xxx
Response includes:
<person>
<distance>-1</distance>
<id>UBAQYFeiHo</id>
<first-name></first-name>
<last-name>Private</last-name>
<headline>Assistant Engineer at Schneider Electric</headline>
<api-standard-profile-request>
<url>http://api.linkedin.com/v1/people/UBAQYFeiHo</url>
<headers total="1">
<http-header>
<name>x-li-auth-token</name>
<value>OUT_OF_NETWORK:wHti</value>
</http-header>
</headers>
</api-standard-profile-request>
</person>
Second call, to get the profile:
http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)
Request headers:
Host: api.linkedin.com
x-li-auth-token: OUT_OF_NETWORK:wHti
accept-encoding: gzip, deflate
user-agent: Python-httplib2/$Rev$
Response:
{'status': '200', 'content-length': '158', 'content-location': u'http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)?oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=27886786&oauth_timestamp=1330098212&oauth_consumer_key=xxx&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=xxx&oauth_signature=xxx', 'transfer-encoding': 'chunked', 'vary': '*', 'server': 'Apache-Coyote/1.1', '-content-encoding': 'gzip', 'date': 'Fri, 24 Feb 2012 15:43:34 GMT', 'x-li-request-id': 'N368G241EA', 'x-li-format': 'xml', 'content-type': 'text/xml;charset=UTF-8'}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<id>UBAQYFeiHo</id>
<first-name></first-name>
<last-name>Private</last-name>
</person>
The python code to make the second part work with the oauth2 library is:
import oauth2 as oauth
import time
url = "http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)"
consumer = oauth.Consumer(
key="xxx",
secret="xxx")
token = oauth.Token(
key="xxx",
secret="xxx")
client = oauth.Client(consumer, token)
resp, content = client.request(url, headers={'x-li-auth-token':'OUT_OF_NETWORK:wHti'})
print resp
print content

Categories

Resources