Trying to send xml file using Python Requests to OpenStreetMap Overpass API -


i have .xml request can succesfully retrieve data openstreetmap overpass api.

<?xml version="1.0" encoding="utf-8"?> <osm-script> <query type="node">     <has-kv k="name" v="bethesda"/>     <has-kv k="network" v="washington metro"/> </query> <query type="way">     <around radius="800"/>     <has-kv k="building"/> </query> <union>     <item/>     <recurse type="down"/> </union> <print/> </osm-script> 

all i'm trying (and failing) send xml via python requests library (i'm open other python solutions). send request below:

files = {'file': ('bethesda.xml', open('bethesda.xml', 'rb'))} r = requests.post('http://overpass-api.de/api/interpreter', data=files) print r.text 

but following error message:

<?xml version="1.0" encoding="utf-8"?> <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>   <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en"/>   <title>osm3s response</title> </head> <body>  <p>the data included in document www.openstreetmap.org. data made available under odbl.</p> <p><strong style="color:#ff0000">error</strong>: line 1: parse error: unknown type "file" </p> <p><strong style="color:#ff0000">error</strong>: line 1: parse error: empty query not allowed </p> <p><strong style="color:#ff0000">error</strong>: line 1: parse error: unknown type "=" </p> <p><strong style="color:#ff0000">error</strong>: line 1: parse error: empty query not allowed </p> <p><strong style="color:#ff0000">error</strong>: line 1: parse error: unknown type "bethesda" </p> <p><strong style="color:#ff0000">error</strong>: line 1: parse error: ';' expected - '&' found. </p> <p><strong style="color:#ff0000">error</strong>: line 2: parse error: unexpected end of input. </p> 

which indicates request reaches overpass api , gets xml file, seems xml request wasn't transferred. i've tried few variations can't better this. clearly, i'm not python...

you want xml body of post. when pass in dict, requests turns url query string isn't encoded correctly , isn't api wants anyway. annoying in opinion. query strings , bodies different beasts - shouldn't smooshed single parameter , automagically guestimated.

this works:

import requests r = requests.post('http://overpass-api.de/api/interpreter',     data=open('bethesda.xml', 'rb')) print(r.text) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -