locked
add json type input parameter in Request body of REST WCF RRS feed

  • Question

  • User946151542 posted

    to add/pass json  type input parameter in Request body of REST WCF.

    The input json type will be having around 40 fields,

    Also this json type parameter comes via Xcode,Objective C Apps i.e, this Rest WCF will be consumed in apple mobile app.This json type needs to  stored in database table.

    via REST Client addon , we're unable to add Json type parameter in request body to check the working of service.

    But the service added as service reference in asp.net application works also works when testing with WCF Client.

    kindly advise.

    Wednesday, May 7, 2014 9:39 PM

Answers

  • User-417640953 posted

    Hi anbujeremiah,

    Thank you post the issue to asp.net forum.

    Based on what you described, I see you want to post json data to wcf rest service using HttpWebRequest.

    For this issue, we should get our object converted to json string, then pass it to wcf rest service through HttpWebRequest like below.

        string uri = string.Format(@"http://localhost:9001/AFMServer" + "/BuildItems");
        var request = HttpWebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";
        //json type data
        string json = JsonConvert.SerializeObject(item);
    
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(json);
            streamWriter.Close();
        }
        request.GetResponse();
    

    And there is a full example for this, please refer to it below.

    http://fromjami.wordpress.com/2013/05/26/call-restful-service-using-httpwebrequest-and-post-data/

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 9, 2014 1:59 AM