0

I'm making the following call using the python requests library:

response = requests.post(
    'https://blockchain-starter.eu-gb.bluemix.net/api/v1/networks/<network id>/chaincode/install',
    headers={
        'accept':        'application/json',
        'content-type':  'multipart/form-data',
        'authorization': 'Basic  ' + b64encode(credential['key'] + ":" + credential['secret'])
    },
    data={
        'chaincode_id':      chaincode_id,
        'chaincode_version': new_version,
        'chaincode_type':    chaincode_type,
        'files':             open('chaincode.zip', 'rb')
    }
)

However when I make a call I get a 500 Internal Server Error (API is this, in particular Peers / Install Chaincode). Given that a call I have earlier to one of the GET endpoints is working correctly I assume there is something wrong with my request, can anyone help?

UPDATE:

Solution was to remove the content-type header and move the file upload into it's own files argument:

response = requests.post(
    https://blockchain-starter.eu-gb.bluemix.net/api/v1/networks/<network id>/chaincode/install,
    headers={
        'accept':        'application/json',
        'authorization': 'Basic  ' + b64encode(credential['key'] + ":" + credential['secret'])
    },
    data={
        'chaincode_id':      chaincode_id,
        'chaincode_version': new_version,
        'chaincode_type':    chaincode_language
    },
    files={
        'file': open('chaincode_id.zip', 'rb')
    }
)
MysteriousWaffle
  • 439
  • 1
  • 5
  • 16
  • 1
    Does [this](https://stackoverflow.com/questions/19439961/python-requests-post-json-and-file-in-single-request) help ? – jar Oct 09 '18 at 18:27
  • Yes! Not the top answer but the second one. To anyone interested, the solution is removing the `content-type` header and moving the files into it's own section. – MysteriousWaffle Oct 09 '18 at 18:36

1 Answers1

1

As acknowledged by the person asking the question, this answer by ralf htp seems to have solved their issue.

Do not set the Content-type header yourself, leave that to pyrequests to generate

def send_request():
payload = {"param_1": "value_1", "param_2": "value_2"}
files = {
     'json': (None, json.dumps(payload), 'application/json'),
     'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream')
}

r = requests.post(url, files=files)
print(r.content)
jar
  • 2,646
  • 1
  • 22
  • 47