Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to send request to visa api with python

I’m trying to send post requests to visa api with python-requests.
I succeeded in sending through visa developer center playground and it’s working. but doin it with python not working.
my code:

url = "https://sandbox.api.visa.com/filedeliveryservice/v1/binFileTransfer"
request_body = {
    "requestHeader": {
        "requestTS": "2017-02-15T22:05:00.000",
        "requestMessageId": "testAnna"
    },
    "requestData": {"fileName": "VBASS_TR_LEVEL3_03262024_3.csv"}
}
r = requests.post(url,
                  cert=(client_cert, client_key),
                  auth=(user_id, password),
                  data=json.dumps(request_body),
                  timeout=10
                  )

the response:

b'{"timestamp":"2024-07-11T06:09:10.260+00:00","status":415,"error":"Unsupported Media Type","path":"/CDISI_VBASS/services/v1/binFileTransfer"}'

what is i am doing wrong, i have no idea yet.
is it even right way to send request? i can’t find any valid source on this theme on internet

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The "Unsupported Media Type" error (HTTP status code 415) indicates that the server is rejecting the request because the Content-Type of the request is not what it expects. For the Visa API, you likely need to set the Content-Type header to application/json when sending a JSON payload.

Updated Code:

import requests
import json

url = "https://sandbox.api.visa.com/filedeliveryservice/v1/binFileTransfer"
request_body = {
    "requestHeader": {
        "requestTS": "2017-02-15T22:05:00.000",
        "requestMessageId": "testAnna"
    },
    "requestData": {"fileName": "VBASS_TR_LEVEL3_03262024_3.csv"}
}

headers = {
    "Content-Type": "application/json"
}

r = requests.post(
    url,
    headers=headers,
    cert=(client_cert, client_key),
    auth=(user_id, password),
    data=json.dumps(request_body),
    timeout=10
)

print(r.text)

The headers dictionary includes Content-Type: application/json to inform the server that the request body is in JSON format.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading