How to get rid of newlines in XML sent via POST-request - python

I'm trying to send .xml file via POST request:
files = {"FileName": open (path_to_file.xml, "rb")}
requests.post(url, files=files)
But file that sent from client side as
<protection>
<create name="domain1/domain2/>
<allowed name="domain1"/>
<allowed name="domain2"/>
</protection>
Received on server side as
<protection>
<create name="domain1/domain2/>
<allowed name="domain1"/>
<allowed name="domain2"/>
</protection>
Did someone faced with such issue?

Related

Telegram Bot SendDocument pdf

I am having a real headache with the way of sending a pdf file to a Telegram Bot.
Apparently I am following the documentation but never get it sent.
I am using the url: https://api.telegram.org/botBOTID/sendDocument?chat_id=CHATID&document=/home/lix/Downloads/2.pdf
It is a pdf file storaged locally, but I think it is just the way I am presenting it.
The error getting is:
{"ok":false,"error_code":400,"description":"Bad Request: URL host is empty"}
Does anybody knows how to send a pdf local file?
Many thanks
You should send a POST request, with the PDF as a payload, using the Python Requests library, your code should look something like this:
import requests
# Url with bot token + user id
url = "https://api.telegram.org/bot<MY-BOT-TOKEN>/sendDocument?chat_id=<MY_CHAT_ID>"
# Create payload with PDF file
payload = {}
files = [
('document', open('/home/lix/Downloads/2.pdf','rb'))
]
headers= {}
# Request
response = requests.request("POST", url, headers=headers, data = payload, files = files)
# Log reponse as UTF-8
print(response.text.encode('utf8'))

Upload a file using python requests

I have a problem trying to upload a file into an API. In the swagger UI I have not problem uploading an excel file manually. When I try tu upload using request, I recive a 415 error (Invalid format of my file). Here is a simple code of that post request:
headers = {
'Authorization':"bearer "+ CLIENT.token,
'Content-Type': 'form-data'
}
files = [('file', open(path_to_file, 'rb'))]
response = requests.post(api_url,
files=files,
headers=headers)
My response has status code 415, I dont Know what is happening. When I used the swagger, I inspected the newtwork browser, and I saw this header in the request
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarywkrdGd3ROh0GkfsW
But, I don't know what is the term "boundary", and if I pass this header manually into the requests, the API throws a 500.
The server is saying that your Content-Type is wrong. If you're uploading a .xls file use:
'Content-Type': 'application/vnd.ms-excel'
If you're uploading a .xlsx file use:
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

How python flask server send back response with XML file

I coded a mock server, after I got a POST message with XML file, I need send back a response message with XML file also.
I found some example as below to send back response with XML content. But I think it's not a file. Anyone has idea on sending back response with a file?
result = open('output.xml', 'r').read()
r = Response(response=result, status=200, mimetype="application/xml")

how to read the response which is a sound file in python?

I am sending a request to the server and server is sending a response back. But the response is in the form of sound file.
working - I am sending the request, response is received on the terminal, so I am writing it to text but later how to convert that into a sound file (.wav). The response from the server is as shown below.
The response as a text in the beginning and the binary in the middle and later text in the end. how to get a sound file out of it ? someone help me in this ?

Send a request to webservice via an xml file?

I have a webservice running at "tcs-webdev2:8200/scheduler/requestgroup" to which new build requests can be sent using an xml file(sample xml file below). I need some guidance
on how requests to a webserive via n xml file work.
Sample xml file:-
<BuildInfo>
<BuildSource>DEV_CI</BuildSource>
<SoftwareProductBuild>MAAAAANLGD0000211.1_101</SoftwareProductBuild>
<PriorrootBuild>MAAAAANLGD0000211.1</PriorrootBuild>
<NewSIBuilds>
<Image>
<Type>LNX</Type>
<SoftwareImageBuild>buildlocation</SoftwareImageBuild>
<Location>\\\sever\buildlocation\checkout</Location>
<Variant>Default</Variant>
<LoadType>Debug</LoadType>
</Image>
</NewSIBuilds>
</BuildInfo>
It depends on your web service, how exactly you need to send the request, but you have to something like the following:
import httplib
with open("your_xml_filename.xml") as f:
body = f.read()
headers = {"Content-type": "application/xml"}
conn = httplib.HTTPConnection("tcs-webdev2", 8200)
conn.request("POST", "/scheduler/requestgroup", body, headers)
response = conn.getresponse()
print( response.status )
print( response.read())
conn.close()
It assumes that tcs-webdev2 is a valid hostname (if not, you can use the IP address). Also this request is an HTTP POST, your service might need different request type. Also some additional headers and authentication might be needed.

Categories

Resources