user1943726
user1943726

Reputation: 75

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

I have an .xml request that can succesfully retrieve data from the 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) to do now is send this xml via the Python requests library (i'm open to other python solutions). I send the 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 get the 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 this document is from www.openstreetmap.org. The data is 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: An empty query is 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: An empty query is 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 the request successfully reaches the Overpass API and gets back an xml file, but it seems like the xml request wasn't successfully transferred. I've tried a few variations but can't do much better than this. Clearly, I'm not much with Python...

Upvotes: 2

Views: 3688

Answers (1)

tdelaney
tdelaney

Reputation: 77347

You want the xml to be the body of the POST. When you pass in the dict, requests turns it into a url query string that isn't encoded correctly and isn't what the api wants anyway. Its very annoying in my opinion. Query strings and bodies are different beasts - they shouldn't be smooshed into a single parameter and automagically guestimated.

This works:

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

Upvotes: 5

Related Questions