python upload file by requests - python

when i use curl to upload data to server everything is ok but when i use requests in python, api server not accepted the request.
Blockquote
curl:
curl -X POST https://tapi.bale.ai/botXXX/Senddocument \
-H 'content-type: multipart/form-data' \
-F chat_id=2040137555 \
-F document=#/var/tmp/2706021400.pdf \
-F caption=caption
requests:
import requests
headers = {
'content-type': 'multipart/form-data',
}
files = {
'chat_id': (None, '2040137555'),
'document': ('/var/tmp/2706021400.pdf', open('/var/tmp/2706021400.pdf', 'rb')),
'caption': (None, 'caption'),
}
response = requests.post('https://tapi.bale.ai/botXXX/Senddocument', headers=headers, files=files)

Related

Convert request from curl to python

I have this request using a curl command and I want to translate it to python using the requests library
curl -X POST https://endpoint/prod/api/Translations/start \
-H 'Authorization: Bearer <accessToken>' \
-H 'Content-Type: application/json' \
-d '{ "text": ["first segment to translate.", "second segment to translate."], "sourceLanguageCode": "en", "targetLanguageCode": "de", "model": "general", "useCase": "testing"}'
You can use requests library.
The following curl:
curl -X POST "https://www.magical-website.com/api/v2/token/refresh/" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"refresh": "$REFRESH_TOKEN"
}'
I wrote in python the following way:
import requests
def get_new_token():
url = 'https://www.magical-website.com/api/v2/token/refresh/'
token = constants.THE_TOKEN
payload = f'{{ "refresh": "{token}" }}'
headers = {"accept": "application/json", "Content-Type": "application/json"}
print("Token handling ....")
r = requests.post(url, data=payload, headers=headers)
print(f"Token status: {r.status_code}")
return r.json()['access']
You can try this one.
import requests
url = "https://endpoint/prod/api/Translations/start"
payload = {
"text": ...,
"sourceLanguageCode": ...,
...
}
headers = { "Authorization": "Bearer ...", "Content-Type": "application/json" }
res = requests.post(url, data = payload, headers = headers)
print(res.status_code)
print(res.text)

CURL POST with Python

I am trying to POST a multipart/base64 xml file to portal using the following in python. How can i run it in Python?
curl -X POST -H 'Accept: application/xml' -H 'Content-Type: multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>' --data-binary #<filename>.xml https://<customer_name>.host.com/cxml/invoices
You can use this website
I got this code. Could you try it ?
import requests
headers = {
'Accept': 'application/xml',
'Content-Type': 'multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>',
}
data = open('filename.xml', 'rb').read()
response = requests.post('https://<customer_name>.host.com/cxml/invoices', headers=headers, data=data)

How to send .csv file using cURL in python

I have API and I need to send a .csv file using cURL in python. I do not know how to write this command on python.
curl --location --request POST 'http://**************' \
--header 'Accept: application/json' \
--header 'API-AUTH-TOKEN: **************' \
--form 'list=#"/C:/Users/1288956/Downloads/ozon_search_query_test.csv"'
means I can't show it
R.J. Adriaansen gave me a good answer: curlconverter.com
import requests
headers = {
'Accept': 'application/json',
'API-AUTH-TOKEN': '**************',
}
files = {
'list': ('"/C:/Users/1288956/Downloads/ozon_search_query_test.csv"', open('"/C:/Users/1288956/Downloads/ozon_search_query_test.csv"', 'rb')),
}
response = requests.post('http:///**************', headers=headers, files=files)

Why does this call work to Postman from Curl but not Python

Curl Code works python does not
trying to change from curl to python
curl --location --request PUT "https://api.getpostman.com/environments/XXXXX-YYYYYY-ZZZZ-BBBB-AAAA-ZZZZZZ?apikey=12334567890" \
--header "Content-Type: application/json" \
--data "{
\"environment\": {
\"values\": [
{\"key\": \"url\", \"value\": \"http://10.12.30.131\"}
]
}
}"
import requests
import json
url = 'https://api.getpostman.com/environments/XXXXX-YYYYYY-ZZZZ-BBBB-AAAA-ZZZZZZ?apikey=12334567890'
header = {"Content-type": "application/json"}
body = '{\"environment\": { \"name\": \"Prod - Deploy\", \"values\": [ {\"key\": \"url\", \"value\": \"http://10.12.30.131\"}]}}'
response = requests.put(url, data=json.dumps(body), headers=header)
print(response.status_code)
print(response.text)
expect a 200 response
the body should be body = '{"environment\": {"name\": "Prod - Deploy\", "values\": [{"key\": "url\", "value\": "http://10.12.30.131\"}, {"key\": "appPath\", "value\": "footytips-apis-v1\"} ]}}'

Insert Calendar using Google API (Python)

I would like to use the Google API to insert a secondary calendar. I've used the Google explanation but can't seem to do it myself. How do you use:
calendar = {
'summary': 'calendarSummary',
'timeZone': 'America/Los_Angeles'
}
created_calendar = service.calendars().insert(body=calendar).execute()
print created_calendar['id']
So if you want to add a secondary calendar this is the curl:
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer here_add_access_token" https://www.googleapis.com/calendar/v3/calendars -d {'summary':'here_calendar_name'}
And in Python is:
import requests
url = "https://www.googleapis.com/calendar/v3/calendars
headers = {
'Content-Type': "application/json",
'Authorization': "Bearer access_token"
}
payload = {
'summary': 'Google Calendar secondary',
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
response = response.text

Categories