Search code examples
c#jsonhttpwebrequestrestsharp

How to get json data in REST endpoint C#


i set the Transaction response callback url of an API to : https://requestb.in/st4fz3st and it gives me a response ok, on debugging the response process by checking the link https://requestb.in/st4fz3st?inspect the results shown in the image below. enter image description here

i want to get the json data so i wrote this c# code

string GET(string url)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    try
    {
        WebResponse response = request.GetResponse();
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            return reader.ReadToEnd();
        }
    }
    catch (WebException ex)
    {
        WebResponse errorResponse = ex.Response;
        using (Stream responseStream = errorResponse.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            return errorText;
        }
        throw;
    }
}
  protected void Page_Load(object sender, EventArgs e)
{
    x = GET(" https://requestb.in/st4fz3st");
    Response.Write(x);
}

But what i get is the ok not the RAW BODY which is marked in red.


Solution

  • You can use the api. For example in your case: https://requestb.in/api/v1/bins/st4fz3st/requests

    You can parse the result with Newtonsoft.Json. The 'raw body' is body property