Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call Web API from controller with multiple json parameters. I don't want to use Ajax call. So how can I do that?

What I have tried:

var jsonData = "{\"jsonrpc\":2.0," +
                "\"method\":eth_blockNumber," +
                "\"params\":[]," +
                "\"id\":83}";

            HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(string.Format("https://mainnet.infura.io/qhggowRXK7HIgXB0NEyw",jsonData));
            webreq.Method = "POST";
            webreq.ContentType = "application/json";
            using (var streamWriter = new StreamWriter(webreq.GetRequestStream()))
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                //var resToWrite = serializer.Deserialize<Dictionary<string, object>>(jsonData);
                //streamWriter.Write(jsonData);
                streamWriter.Flush();
                streamWriter.Close();
            }
            WebResponse response = webreq.GetResponse();
            var streamReader = new StreamReader(response.GetResponseStream());
            var result = streamReader.ReadToEnd();
Posted
Updated 3-Jan-19 15:30pm
Comments
F-ES Sitecore 27-Jun-18 6:19am    
What's wrong with the code you posted?
F-ES Sitecore 27-Jun-18 6:48am    
You don't think the fact that it is throwing an error, what line the error is thrown on, or what error is thrown is relevant to your question?
Hardik Dhankecha 27-Jun-18 7:46am    
This error I get when I run it.

An error has occurred.The remote server returned an error: (400) Bad Request.System.Net.WebException at System.Net.HttpWebRequest.GetResponse() at NeoAPIProject.Controllers.NeoAPIController.ETHAddressListen()
F-ES Sitecore 27-Jun-18 8:30am    
You're not actually sending your json data with the POST, this code is doing nothing

string.Format("https://mainnet.infura.io/qhggowRXK7HIgXB0NEyw",jsonData)

Have a look at the link below which shows how you send data with your post, in the below example it's not json but that doesn't matter, the code is still the same, just make sure you use the right content type too.

https://technet.rapaport.com/Info/Prices/SampleCode/Full_Example.aspx
Hardik Dhankecha 27-Jun-18 8:38am    
@F-ES Thank you
Let me check this once....


1 solution

I think this will work.
C#
var httpWebRequest=(HttpWebRequest)WebRequest.Create("https://mainnet.infura.io/qhggowRXK7HIgXB0NEyw");
httpWebRequest.ContentType="application/json";
httpWebRequest.Method="POST";
using(var streamwriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
   string jsonData = "{\"jsonrpc\":2.0," + "\"method\":eth_blockNumber," +
                "\"params\":[]," + "\"id\":83}";
   streamWriter.Write(jsonData);
   streamWriter.Flush();
   streamWriter.Close();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900