0

I am trying to convert

curl -d '[[51.3, 13.4], [51.4, 13.3]]' -XPOST  -H 'Content-Type: application/json'  https://elevation.racemap.com/api

Curl command into Python. I tried

import urllib.request
import json      

body = {'locs': [[51.3, 13.4], [51.4, 13.3]]}
myurl = "https://elevation.racemap.com/api"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8')   
req.add_header('Content-Length', len(jsondataasbytes))
print (jsondataasbytes)
response = urllib.request.urlopen(req, jsondataasbytes)

which gives an error. It seems curl does not specify parameter name while passing an array? I am not sure how to form the Json to fit the curl input.

2
  • 1
    "an error" is the worst of all errors, since nobody can help you with it. Please add a detailed error description including the full error traceback. Commented Apr 25, 2019 at 10:48
  • You can paste your curl command into curlconverter.com and it will convert it to Python code using Requests for you. Commented Sep 20, 2022 at 5:19

1 Answer 1

1

I would simply just use requests:

import requests

headers = {
    'Content-Type': 'application/json',
}

data = '[[51.3, 13.4], [51.4, 13.3]]'

response = requests.post('https://elevation.racemap.com/api', headers=headers, data=data)

print(response.status_code)
# 200
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.