locked
WCF Restful services for POST METHOD uisng json and how call from client application and return the response RRS feed

  • Question

  • User-1136442952 posted

    Please can any baby help how create wcf restful service for post method and how to return the response using json deserialization. I have done like this 

    var body = HttpContentExtensions.CreateDataContract(ObjIncident);
    HttpResponseMessage request = client.Send(HttpMethod.POST, "ClaimCreateIncidient", body);
    request.EnsureStatusIsSuccessful();

    Stream data = request.Content.ReadAsStream();
    StreamReader reader = new StreamReader(data);
    string str = string.Empty;
    str = reader.ReadLine();
    JavaScriptSerializer ser = new JavaScriptSerializer();
    List<CreateIncident> dt = ser.Deserialize<List<CreateIncident>>(str);

    but during deserialization am getting INVALID JSON primitive error please help me

    Monday, May 20, 2013 2:36 AM

Answers

  • User260886948 posted

    Hi,
    Please try to like blow:

    Here is operation contract:

    [OperationContract]
    [WebGet(UriTemplate = "Students/", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
    public List<Student> FetchStudents()
    {
    //Fetch and return students list
    }

    Client code:

    static void Main(string[] args)
    {
    HttpClient client = new HttpClient("http://localhost/StudentManagementService/StudentManagement.svc/");
    response = client.Get("Students/");
    response.EnsureStatusIsSuccessful();
    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    string str = response.Content.ReadAsString();
    List<Student> st = json_serializer.Deserialize<List<Student>>(str);
    }

    Hope it can help you.

    Best Regards,
    Amy Peng

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 27, 2013 1:19 AM