简体   繁体   中英

CURL API call working but PYthon requests Post call gets no response

I am trying to call the FPML validate API. I got the API call to work via CURL successfully.

curl -i -w '\n' -H "x-api-key:MY_KEY" -H "Content-Type:text/xml" https://gvtumr1txd.execute-api.eu-west-1.amazonaws.com/production/validate --data @SampleFile_ValuationReport_IRS.xml \ --trace-ascii -

Heres my python function for the same using the reqeusts library

    def validate_raw(self, raw_file):
        """
        validates a raw FpML file using FpML.org validation API
        raw_file is a path object in python
        """
        #call the FpML validator API from FpML.org
        headers={}
        headers["x-api-key"] = "MY_KEY" 
        headers["Content-Type"]="text/xml"
        with open(raw_file) as file:
            response = requests.post("https://gvtumr1txd.execute-api.eu-west-1.amazonaws.com/production/validate", data=file,headers=headers)  
        print(response.status_code)
        return response

I call this from my main.py file

from pathlib import Path
from DataIngestor import RawDataIngestor
from Utilities import FileHandlingUtilities

rel_path="../Data"
filename="SampleFile_ValuationReport_IRS.xml"
file_path=Path(rel_path)
raw_file=file_path/filename
#remove Byte order marks from the file if present. FPML.org APIs do not handle BOM characters
FileHandlingUtilities.remove_bom_inplace(raw_file)
data_ingestor=RawDataIngestor.Fpml_Ingestor()
validation_response=data_ingestor.validate_raw(raw_file)
print(validation_response.text)

Heres the error message. I have turned logging on so you can see whats being send to the API call

Reloaded modules: DataIngestor, DataIngestor.RawDataIngestor, Utilities, Utilities.FileHandlingUtilities
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gvtumr1txd.execute-api.eu-west-1.amazonaws.com:443
send: b'POST /production/validate HTTP/1.1\r\nHost: gvtumr1txd.execute-api.eu-west-1.amazonaws.com\r\nUser-Agent: python-requests/2.27.1\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nx-api-key: FEUmuAR83D5bwA5JkASyd8LGczAhpgXU5GL3G2M5\r\nContent-Type: text/xml\r\nContent-Length: 2809\r\n\r\n'
sendIng a read()able
encoding file using iso-8859-1
send: b'<?xml version="1.0" encoding="utf-8"?>\n<valuationReport xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:fpml-annotation="http://www.fpml.org/annotation" xmlns="http://www.fpml.org/FpML-5/reporting" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/FpML-5/reporting file:///C:/Users/gabriel%20thomas/Documents/GitHub/GitLab/frtbsa/Static/Schemas/XML/reporting-5-12_xml/reporting/fpml-valuation-reporting-5-12.xsd" fpmlVersion="5-0">\n    <header>\n        <messageId messageIdScheme="gabt_frtbsa_fpml">MSG_VALREP_0000001</messageId>\n    </header>\n    <party id="party1">\n        <partyId partyIdScheme="http://www.fpml.org/coding-scheme/dummy-party-id">Party A</partyId>\n    </party>\n    <tradeValuationItem>\n        <partyTradeIdentifier id="tid-1">\n            <partyReference href="party1" />\n            <tradeId tradeIdScheme="gabt_frtbsa_TradeIDScheme">T00000001</tradeId>\n        </partyTradeIdentifier>\n        <valuationSet id="val1">\n            <valuationScenario id="valscen1">\n                <name>EOD Valuation</name>\n                <valuationDate>2022-01-08</valuationDate>\n            </valuationScenario>\n            <baseParty href="party1" />\n            <!--results for trade #1-->\n            <assetValuation>\n                <objectReference href="tid-1" />\n                <!--quote value and NPV-->\n                <quote>\n                    <value>-14875</value>\n                    <measureType>NPV</measureType>\n                    <sensitivitySet>\n                        <name>Interest Rate Delta Sensitivity in $/bp</name>\n                        <sensitivity name="1D">211</sensitivity>\n                        <sensitivity name="2D">212</sensitivity>\n                        <sensitivity name="1M">213</sensitivity>\n                        <sensitivity name="3M">214</sensitivity>\n                        <sensitivity name="DEC03">215</sensitivity>\n                        <sensitivity name="MAR04">216</sensitivity>\n                        <sensitivity name="JUN04">217</sensitivity>\n                        <sensitivity name="SEP04">218</sensitivity>\n                        <sensitivity name="2Y">219</sensitivity>\n                        <sensitivity name="3Y">220</sensitivity>\n                        <sensitivity name="4Y">221</sensitivity>\n                        <sensitivity name="5Y">222</sensitivity>\n                        <sensitivity name="7Y">223</sensitivity>\n                        <sensitivity name="10Y">224</sensitivity>\n                        <sensitivity name="15Y">225</sensitivity>\n                    </sensitivitySet>\n                </quote>\n            </assetValuation>\n        </valuationSet>\n    </tradeValuationItem>    \n</valuationReport>'
reply: ''
Traceback (most recent call last):

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 710, in urlopen
    chunked=chunked,

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)

  File "<string>", line 3, in raise_from

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()

  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\client.py", line 1369, in getresponse

  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\client.py", line 310, in begin

  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\client.py", line 279, in _read_status

RemoteDisconnected: Remote end closed connection without response


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Program Files\Spyder\pkgs\requests\adapters.py", line 450, in send
    timeout=timeout

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 786, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

  File "C:\Program Files\Spyder\pkgs\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)

  File "C:\Program Files\Spyder\pkgs\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 710, in urlopen
    chunked=chunked,

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)

  File "<string>", line 3, in raise_from

  File "C:\Program Files\Spyder\pkgs\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()

  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\client.py", line 1369, in getresponse

  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\client.py", line 310, in begin

  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\client.py", line 279, in _read_status

ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\gabriel thomas\Documents\GitHub\GitLab\frtbsa\Modules\main.py", line 18, in <module>
    validation_response=data_ingestor.validate_raw(raw_file)

  File "C:\Users\gabriel thomas\Documents\GitHub\GitLab\frtbsa\Modules\DataIngestor\RawDataIngestor.py", line 64, in validate_raw
    response = requests.post("https://gvtumr1txd.execute-api.eu-west-1.amazonaws.com/production/validate", data=file,headers=headers)

  File "C:\Program Files\Spyder\pkgs\requests\api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)

  File "C:\Program Files\Spyder\pkgs\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)

  File "C:\Program Files\Spyder\pkgs\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)

  File "C:\Program Files\Spyder\pkgs\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)

  File "C:\Program Files\Spyder\pkgs\requests\adapters.py", line 501, in send
    raise ConnectionError(err, request=request)

ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

Please can someone help with suggestions.

posting @dave_thompson_085 comment as the answer. it worked for me!

Since you appear to be on Windows, where text files and binary files are different, see the Warning at docs.python-requests.org/en/master/user/advanced/…. – dave_thompson_085 1 hour ago

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM