I am trying to execute a curl GET request with python3
the request is:
curl -k -X GET -H "X-Some-Token: s.ggt5gvf344f"https://ip.address:port/v1/path/path
how it can be implemented?
import requests
url = "https://ip.address:port/v1/path/path"
payload={}
headers = {
'X-Some-Token': 's.ggt5gvf344f'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
or you can try http.client library which is much faster than requests
import http.client
conn = http.client.HTTPSConnection("ip.address", port)
payload = ''
headers = {
'X-Some-Token': 's.ggt5gvf344f'
}
conn.request("GET", "/v1/path/path", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Related
I have below curl command
file_location='some/path/to/test.csv'
auth_token='my_api_token'
curl --insecure --request POST 'https://<ip_address>/eds/api/table/table_name/changes/addBulk?hasHeader=false' -H 'Csrf-Token: nocheck' -H 'AuthToken:'$auth_token'' -H 'Content-Type: multipart/form-data' --form 'data=#'$file_location'
The data in csv is just multiple rows with 1 column, for example below:
row_1
row_2
row_3
The curl command works perfectly fine, I am trying to get python alternative for it, I tried below:
files = {'file': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')}
auth_token="<api token here>"
url="https://" + ip_address +"/eds/api/table/" + table_name + "/changes/addBulk?hasHeader=false"
headers = {
'Csrf-Token': 'nocheck',
'AuthToken': auth_token
}
response = requests.post(url, headers=headers, files=files, verify=False)
Any help is appreciated
I noticed in your curl url you have "eds" before "/api/..." but in your python version you do not.
curl_url = "https://<ip_address>/eds/api/table/table_name/changes/addBulk?hasHeader=false"
python_url = "https://" + ip_address +"/api/table/" + table_name + "/changes/addBulk?hasHeader=false"
Also, in python 3 it is cleaner to use an f-string like this:
url = f"https://{ip_address}**/eds/**api/table/{table_name}/changes/addBulk?hasHeader=false"
Everything else looks right to me. In the future it may help to set a break point on the response to see if there is any additional information with the 400 http response.
EDIT:
Okay then, can you try modifying your header to be:
headers = {
'Csrf-Token': 'nocheck',
'AuthToken': auth_token,
'Content-Type': 'multipart/form-data'
}
Also edit your files to be:
files = {
'data': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')
}
Final code should be:
auth_token = "<api token here>"
files = {
'data': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')
}
url = f"https://{ip_address}/eds/api/table/{table_name}/changes/addBulk?hasHeader=false"
headers = {
'Csrf-Token': 'nocheck',
'AuthToken': auth_token
}
response = requests.post(url, headers=headers, files=files, verify=False)
I am unable to access an API.
This is what I am doing:
import os, re
import requests
import logger
from requests_oauthlib import OAuth1
oauth_consumer_key = "123abc"
oauth_secret = "aaabbbccc"
url = "https://example_testing/v1/<ID_VALUE>/reports?"
oauth = OAuth1(oauth_consumer_key, oauth_secret)
output = requests.get(url,auth=oauth)
I keep getting a 500 error.
What am I doing wrong?
I didn't know this but apparently I needed to set values for headers and the payload. This solved my problem.
headers = {"Content-Type": "application/json"}
payload = json.dumps({
"report_format":"csv",
<other stuff>
}
output = requests.get(url=url, auth=oauth, headers=headers, data=payload)
Problem Solved
Im trying To Make post requests (multipart/form-data) in this website https://gofile.io/?t=api
every time time i got an error when i try to upload file
my code
import requests
req = requests.session()
files= {'file': open("test.txt", 'rb')}
response = req.post('https://srv-file7.gofile.io/upload', files=files)
print(response.text)
I got error every time ,are smething missing in the code
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import json
mp_encoder = MultipartEncoder(
fields={
'filesUploaded': ('file_name.txt', open('file_name.txt', 'rb'))
}
)
r = requests.post(
'https://srv-file9.gofile.io/upload',
data=mp_encoder,
headers={'Content-Type': mp_encoder.content_type}
)
scrap = r.json()
# send "Token file" 123Abc
Token = scrap['data']['code']
Check this. It will work.
I need to upload a local picture to an URL.
My curl request looks like : "curl -T 'my_picture' 'the_url' ".
My purpose is to do it in Python, I saw, that I have to use : requests.put().
Everything I did before works nice, but this function give me a lot of trouble.
Solution is probably really easy, but I'm lost.
Thanks for any help.
def apithree(urlupload):
url = urlupload
picture = Image.open("test.png")
headers = {"content-type": "multipart/form-data"}
response = requests.put(url, headers=headers, data=picture)
response = response.json()
print(response)
Other Code I tried
def apithree(urlupload):
url = urlupload
files = {'image': open('test.png', 'rb')}
response = requests.post(url, data=files)
response = response.json()
print(response)
If the command works,the output should be empty, but I always have error messages.
I can add error messages if necessary.
Thanks for any help.
If server accepts multipart/form-data images, you probably need files= parameter for your request:
import requests
files = {'file_name': open(r'C:\data\...\image.png', 'rb')}
r = requests.post(YOUR_URL, files=files) # or requests.put(...)
print(r.status_code, r.json())
The following curl command works as intended. Uses POST to send a bit of information to a web site.
curl 'https://...' \
-X PUT \
-d "submission[posted_grade]=65" \
-H "Authorization: Bearer 10~X"
What should be the equivalent python3 code, gives an Error 500: Internal Server Error
url_string = 'https://...'
data = "submission[posted_grade]=40"
data = data.encode('utf-8')
req = urllib.request.Request(url_string, data)
req.add_header("Authorization", "Bearer 10~X")
req.add_header("Content-Type", "application/json")
response = urllib.request.urlopen(req)
print(response.read())
Already tried
data = {'submission[posted_grade]': '40'}
data = json.dumps(data)
data = data.encode('utf-8')
Which gives "HTTP Error 422: Unprocessable Entity"
Any ideas on fixing it?
Thanks #cody
Lesson learned, PUT != POST
The following code works as expected:
url_string = 'https://...'
data = "submission[posted_grade]=40"
data = data.encode('utf-8')
req = urllib.request.Request(url=url_string, data=data, method='PUT')
req.add_header("Authorization", "Bearer 10~X")
response = urllib.request.urlopen(req)