You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to psf/requests#1997 when using files in requests, one is not supposed to provide headers, so that the library will take care of the multipart/form-data with the appropriate content-type.
An example: (we wrote a custom extension just to do the XRAY API import)
Will result in: requests.exceptions.HTTPError: 415 Client Error: for url: https://[REDACTED]/rest/raven/1.0/api/import/execution/robot?projectKey=TCHCSIPDEV
This is due to the implementation in rest_client.py:
defrequest(
self,
method="GET",
path="/",
data=None,
json=None,
flags=None,
params=None,
headers=None,
files=None,
trailing=None,
absolute=False,
advanced_mode=False,
):
""" :param method: :param path: :param data: :param json: :param flags: :param params: :param headers: :param files: :param trailing: bool - OPTIONAL: Add trailing slash to url :param absolute: bool, OPTIONAL: Do not prefix url, url is absolute :param advanced_mode: bool, OPTIONAL: Return the raw response :return: """url=self.url_joiner(Noneifabsoluteelseself.url, path, trailing)
params_already_in_url=Trueif"?"inurlelseFalseifparamsorflags:
ifparams_already_in_url:
url+="&"else:
url+="?"ifparams:
url+=urlencode(paramsor {})
ifflags:
url+= ("&"ifparamsorparams_already_in_urlelse"") +"&".join(flagsor [])
json_dump=NoneiffilesisNone:
data=Noneifnotdataelsedumps(data)
json_dump=Noneifnotjsonelsedumps(json)
self.log_curl_debug(
method=method,
url=url,
headers=headers,
data=dataifdataelsejson_dump,
)
headers=headersorself.default_headers# <------- Always provides headersresponse=self._session.request(
method=method,
url=url,
headers=headers, # <------- Should be None when providing files, so that the requests library handles file upload with appropriate headersdata=data,
json=json,
timeout=self.timeout,
verify=self.verify_ssl,
files=files,
proxies=self.proxies,
cert=self.cert,
)
response.encoding="utf-8"log.debug("HTTP: %s %s -> %s %s", method, path, response.status_code, response.reason)
log.debug("HTTP: Response text -> %s", response.text)
ifself.advanced_modeoradvanced_mode:
returnresponseself.raise_for_status(response)
returnresponse
According to psf/requests#1997 when using files in requests, one is not supposed to provide headers, so that the library will take care of the
multipart/form-data
with the appropriate content-type.An example: (we wrote a custom extension just to do the XRAY API import)
Will result in:
requests.exceptions.HTTPError: 415 Client Error: for url: https://[REDACTED]/rest/raven/1.0/api/import/execution/robot?projectKey=TCHCSIPDEV
This is due to the implementation in
rest_client.py
:If we change this to:
Then the request will work, as the
multipart/form-data
is appropriately handled by the requests library.This is necessary in APIs like XRAY, where one can import result files, which are not JSON.
But it should also be applicable for JSON files.
The text was updated successfully, but these errors were encountered: