Open
Description
It appears the BAN API does not allow the "=" character within the multipart form boundary.
It appears this character should be allowed as referenced here (though comments on this post mention subtilities linked to the differents OSs). This would appear anecdotic but it can have serious repercussions (for instance, requests-cache needed a patch which is not yet released).
Code sample to reproduce the error:
import io
import pandas as pd
import requests
from contextlib import contextmanager
from urllib3 import filepost
csv_sample = """
ID;ENSEIGNE;ADRESSE;COMPLEMENT;CODE POSTAL;COMMUNE;NATURE DU DEBIT
1;;32 route des grands champs;TABAC;01400;L-ABERGEMENT-CLEMENCIAT;Ordinaire permanent
2;;42 RUE ROGER VAILLANT;CC DAME LOUISE-TABAC-;01500;AMBERIEU-EN-BUGEY;Ordinaire permanent
3;TABAC DE LA GARE;6 AV DU GENERAL SARRAIL;TABAC;01500;AMBERIEU-EN-BUGEY;Ordinaire permanent
4;MAISON DE LA PRESSE;14 R BERARD;TABAC;01500;AMBERIEU-EN-BUGEY;Ordinaire permanent
5;;8 AV PAUL PAINLEVE;TABAC;01500;AMBERIEU-EN-BUGEY;Ordinaire permanent
"""
df = pd.read_csv(io.StringIO(csv_sample), sep=";")
files = [
(
"data",
(
"data.csv",
df.to_csv(index=False, encoding="utf8", sep=";"),
"text/csv; charset=utf-8",
),
),
]
endpoint = "https://api-adresse.data.gouv.fr/search/csv/"
# réusside avec un form boundary standard
with requests.Session() as s:
# Ce
r1 = s.post(endpoint, files=files)
print(r1)
print("form boundary was %s", r1.request.body.split()[-1:])
with requests.Session() as s:
# échec avec un form boundary comprenant des "="
@contextmanager
def patch_form_boundary():
original_boundary = filepost.choose_boundary
filepost.choose_boundary = lambda: "====test===="
yield
filepost.choose_boundary = original_boundary
with patch_form_boundary():
r2 = s.post(endpoint, files=files)
print(r2)
print("form boundary was %s", r2.request.body.split()[-1:])
@contextmanager
def other_patch_form_boundary():
original_boundary = filepost.choose_boundary
filepost.choose_boundary = lambda: "test"
yield
filepost.choose_boundary = original_boundary
# mais réussite dès qu'on retire les "="
with other_patch_form_boundary():
r3 = s.post(endpoint, files=files)
print(r3)
print("form boundary was %s", r3.request.body.split()[-1:])
Metadata
Metadata
Assignees
Labels
No labels