Golide
Golide

Reputation: 1009

400 Bad Request for WebRequest over https

When I make a request to the endpoint https://api.mymoney.co.zw/api/v1/addcart in Visual Studio Code 2017 I am getting an exception : The remote server returned an error: (400) Bad Request.. This is only happening within context of VS2017.

My code :

public static void Main(string[] args)
    {
       GetStatistics();
      
    } 
    static async Task<List<Root2>> GetStatistics()
    {
        List<Root2> details = GetRemmitances();                  
        List<Root2> values2 = details.Take(1).ToList();
        
        string responsePath = @"C:\ABC\";
        string responseLog = "ABC_Response";
        var responsePathabs = responsePath + Convert.ToString(responseLog) + DateTime.Today.ToString("dd -MM-yy") + ".txt";
       
        // ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.mymoney.co.zw/api/v1/addcart");
        ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("x-api-key", Helpers.apikey.ToString());
        httpWebRequest.Method = "POST";
           
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
            try
            {
                foreach (var tran in values2)
                {
                    var lis = JsonConvert.SerializeObject(tran);                                              
                    streamWriter.Write(tran);
                    streamWriter.Flush();
                    streamWriter.Close();
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                  

                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    resuit = streamReader.ReadToEnd();
                    
                    if (!File.Exists(responsePathabs))
                    {
                        File.Create(responsePathabs).Dispose();
                    }
                    using (StreamWriter sw = File.AppendText(responsePathabs))
                    {
                        sw.WriteLine(resuit);
                        sw.Flush();
                         sw.Close();
                    }
                }
               
            }

        }
                catch (WebException ex)
        {
            ExceptionLogger.SendErrorToText(ex);
        }

}

I have put a breakpoint and copied the payload in lis to Postman - it executes successfully with no issues.

What am I missing ?

Is this something particular to the context of visual studio or ? I have tried some suggestions i.e to place ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12 before instantatuion of the WebRequest but its still yielding same error.

My code is a Console Program running off a corporate network - all proxies however are disabled for the call.

Upvotes: 0

Views: 850

Answers (1)

John Glenn
John Glenn

Reputation: 1629

You are writing tran to the stream instead of the serialized JSON data in lis.

var lis = JsonConvert.SerializeObject(tran);
streamWriter.Write(tran); // <- should be lis instead of tran

Upvotes: 4

Related Questions