Gewusst wie: senden einer HTTP-POST mit Python 2.7

Ich versuche zum senden einer HTTP-POST-request mit Python. Kann ich es auf der Arbeit mit 3.0, aber ich konnte Sie nicht finden, ein gutes Beispiel dafür, 2.7.

hdr = {"content-type": "application/json"}
payload= ("<html><body><h1>Sorry it's not Friday yet</h1> </body></html>")
r = requests.post("http://my-url/api/Html", json={"HTML": payload})

with open ('c:/temp/a.pdf', 'wb') as f:
    b64str = json.loads(r.text)['BinaryData']  #base 64 string is in BinaryData attr
    binStr = binascii.a2b_base64(b64str)  #convert base64 string to binary
    f.write(binStr)

Die api ist ein json-in diesem format:

{
  HTML : "a html string"
}

und gibt ein json-in diesem format:

{
    BinaryData: 'base64 encoded string'      
}
InformationsquelleAutor Ray Cheng | 2017-04-12
Schreibe einen Kommentar