BAD REQUEST for both General assests and Foreign Exchange Rates API

Ehi
Dabbler

BAD REQUEST for both General assests and Foreign Exchange Rates API

Hello,

I created a number of APIs as I just signed up to VISA developer account and trying to get my first API tested.

 

I created a FOREIGN EXCHANGE RATES API and in the API reference in this page below, I am trying to test the POST in my C# Swagger API

https://developer.visa.com/capabilities/foreign_exchange/reference#tag/Foreign-Exchange-Rates-API/op...

 

but getting BAD REQUEST, my simple code is below

 

 

2. Secondly, I downloaded the sample C# in my DASHBOARD >> GENERAL ASSESTS ( below) and updated with my userID, password and digital cert and again getting bad request

 

My code is below

 

 

/*
* (c) Copyright 2018 - 2020 Visa.All Rights Reserved.**
*
* NOTICE: The software and accompanying information and documentation(together, the "Software") remain the property of and are proprietary to Visa and its suppliers and affiliates.The Software remains protected by intellectual property rights and may be covered by U.S.and foreign patents or patent applications.The Software is licensed and not sold.*
*
* By accessing the Software you are agreeing to Visa's terms of use (developer.visa.com/terms) and privacy policy (developer.visa.com/privacy).In addition, all permissible uses of the Software must be in support of Visa products, programs and services provided through the Visa Developer Program (VDP) platform only (developer.visa.com). **THE SOFTWARE AND ANY ASSOCIATED INFORMATION OR DOCUMENTATION IS PROVIDED ON AN "AS IS," "AS AVAILABLE," "WITH ALL FAULTS" BASIS WITHOUT WARRANTY OR CONDITION OF ANY KIND. YOUR USE IS AT YOUR OWN RISK.** All brand names are the property of their respective owners, used for identification purposes only, and do not imply product endorsement or affiliation with Visa. Any links to third party sites are for your information only and equally do not constitute a Visa endorsement. Visa has no insight into and control over third party content and code and disclaims all liability for any such components, including continued availability and functionality. Benefits depend on implementation details and business factors and coding steps shown are exemplary only and do not reflect all necessary elements for the described capabilities. Capabilities and features are subject to Visa's terms and conditions and may require development,implementation and resources by you based on your business and operational details. Please refer to the specific API documentation for details on the requirements, eligibility and geographic availability.*
*
* This Software includes programs, concepts and details under continuing development by Visa. Any Visa features, functionality, implementation, branding, and schedules may be amended, updated or canceled at Visa"s discretion.The timing of widespread availability of programs and functionality is also subject to a number of factors outside Visa's control, including but not limited to deployment of necessary infrastructure by issuers, acquirers, merchants and mobile device manufacturers.*
*
*/
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;


namespace HelloWorld
{

class Program
{
public static string visaUrl = "https://sandbox.api.visa.com/";
public static string userId = "xxxxxxxxxxxxxxxx";
public static string password = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

public static string cert = "C:\\Users\\User\\Downloads\\cert.pem";
public static string certPassword = "mypassword";

static void Main(string[] args)
{
Console.WriteLine("START Sample Code for Two-Way (Mutual) SSL!");

Program program = new Program();
program.DoMutualAuthCall("vdp/helloworld", "GET", null, null, null);

Console.WriteLine("END Sample Code for Two-Way (Mutual) SSL!");
}

private void LogRequest(string url, string requestBody)
{
Debug.WriteLine(url);
Debug.WriteLine(requestBody);
}

private void LogResponse(string info, HttpWebResponse response)
{
string responseBody;
Debug.WriteLine(info);
Debug.WriteLine("Response Status: \n" + response.StatusCode);
Debug.WriteLine("Response Headers: \n" + response.Headers.ToString());

using (var reader = new StreamReader(response.GetResponseStream(), ASCIIEncoding.ASCII))
{
responseBody = reader.ReadToEnd();
}

Debug.WriteLine("Response Body: \n" + responseBody);
}

private string GetBasicAuthHeader(string userId, string password)
{
string authString = userId + ":" + password;
var authStringBytes = Encoding.UTF8.GetBytes(authString);
string authHeaderString = Convert.ToBase64String(authStringBytes);
return "Basic " + authHeaderString;
}

public string DoMutualAuthCall(string path, string method, string testInfo, string requestBodyString, Dictionary<string, string> headers = null)
{
string requestURL = visaUrl + path;
string certificatePath = cert;
string certificatePassword = certPassword;
string statusCode = "";
LogRequest(requestURL, requestBodyString);
// Create the POST request object
HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
request.ContentType = "application/json";
request.Accept = "application/json";

request.Method = method;
if (method.Equals("POST") || method.Equals("PUT"))
{
// Load the body for the post request
var requestStringBytes = Encoding.UTF8.GetBytes(requestBodyString);
request.GetRequestStream().Write(requestStringBytes, 0, requestStringBytes.Length);
}

if (headers != null)
{
foreach (KeyValuePair<string, string> header in headers)
{
request.Headers[header.Key] = header.Value;
}
}

// Add headers
request.Headers["Authorization"] = GetBasicAuthHeader(userId, password);

// Add certificate
var certificate = new X509Certificate2(certificatePath, certificatePassword);
request.ClientCertificates.Add(certificate);

try
{
// Make the call
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
LogResponse(testInfo, response);
statusCode = response.StatusCode.ToString();
}
}
catch (WebException e)
{
if (e.Response is HttpWebResponse)
{
HttpWebResponse response = (HttpWebResponse)e.Response;
LogResponse(testInfo, response);
statusCode = response.StatusCode.ToString();
}
}
return statusCode;
}

}
}

 

 

 

SWAGGER API IS BELOW

Documentation can be found here 

https://developer.visa.com/capabilities/foreign_exchange/reference#tag/Foreign-Exchange-Rates-API/op...

 

 

[HttpPost]
[Route("ForeignExchangeRatesAPI")]
public async Task<IActionResult> ForeignExchangeRatesAPI()
{
try
{
// Visa API Endpoint
Uri apiUrl = new Uri("https://sandbox.api.visa.com/forexrates/v2/foreignexchangerates");

// JSON Payload
var payLoad = new
{
initiatingPartyId = 1002,
rateProductCode = "BANK",
destinationCurrencyCode = "USD",
sourceCurrencyCode = "EUR",
quoteIdRequired = true
};


// Convert payload to JSON
string jsonPayload = JsonConvert.SerializeObject(payLoad);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

// Add authentication headers (API Key or other method)
// _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_ACCESS_TOKEN");

// Make HTTP POST request
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, content);

// Read response
string responseString = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
{
return Ok(JsonConvert.DeserializeObject(responseString));
}
else
{
return BadRequest($"Error: {response.StatusCode}, Details: {responseString}");
}
}
catch (Exception ex)
{
return StatusCode(500, $"Internal Server Error: {ex.Message}");
}
}

1 REPLY 1
SyedSa
Community Moderator

Re: BAD REQUEST for both General assests and Foreign Exchange Rates API

Hi @Ehi, Thank you for reaching out! An agent will get back to you as soon as possible. Until then, if any community member knows a solution, feel free to reply in this thread.