-1

Here is the API from this website

curl -X POST -F data=@path/to/file.csv https://api-adresse.data.gouv.fr/search/csv/

I would like to know how to use this in python.

What I currently know is that from the same website, we also have this API

curl "https://api-adresse.data.gouv.fr/search/?q=8+bd+du+port"

With python we can do:

import requests
ADDOK_URL = 'http://api-adresse.data.gouv.fr/search/'
response = requests.get(ADDOK_URL, params={'q': '8 bd du port', 'limit': 5})
response.json()

But with

curl -X POST -F data=@path/to/file.csv https://api-adresse.data.gouv.fr/search/csv/

I have to specify a path of csv file. And I also don't know what are -X POST -F.

John Smith
  • 1,604
  • 4
  • 18
  • 45
  • You seem to have the building blocks together. Without an API description and/or task what you wnat to do, I don't see how to help you, or what the question is ... – Cpt.Hook Nov 29 '22 at 13:01
  • You seem to have answered your own question. Assuming the GET returns HTTP 200 then your call to response.json() will produce the desired results – DarkKnight Nov 29 '22 at 13:07
  • Just a guess here, but judging by the `curl` command, I think you should replace `{'q': '8 bd du port',` with `{'q': '8+bd+du+port',`. – accdias Nov 29 '22 at 13:12
  • Does this answer your question? [Python : Trying to POST form using requests](https://stackoverflow.com/questions/20759981/python-trying-to-post-form-using-requests) – accdias Nov 29 '22 at 13:13
  • From the `curl` command `-X POST` is the equivalent of `requests.post()`, and `-F data=@path/to/file.csv` I guess you can pass `data={'data': '@path/to/file.csv'}` to `post()`. – accdias Nov 29 '22 at 13:20

1 Answers1

0

This worked for me, but I don't know what type of response you are expercting. I got no errors and some values in a test as a result.

import requests

files = [
    ('data', ('file', open('your path to .csv file', 'rb'), 'application/octet-stream'))
]

response = requests.post("https://api-adresse.data.gouv.fr/search/csv/", files=files, params={'q': '8 bd du port', 'limit': 5})

print(response.text)

This is a POST request, you used a GET request, so use requests.post and not requests.get.