Making a post request with data in

Posted By

Making a post request with data in

One of the major challenge is to make a post request to an api with data.

Most important for this is to serialize the post data in proper format.

Bellow is the basic syntax to create a api post request and getting the response.

            Dim postdata As New JObject
            postdata.Add("service_id", "value 1")
            postdata.Add("template_id", "Value 2")
            postdata.Add("user_id", "Value 3")
            'postdata contains data which will be posted with the request
            Dim finalString as String = postdata.ToString

            Dim httpWebRequest = CType(WebRequest.Create("Api address Here"), HttpWebRequest)
            httpWebRequest.ContentType = "application/json"
            httpWebRequest.Method = "POST"

            Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
            End Using

            Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)

            Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                Dim responseText as String = streamReader.ReadToEnd() 
                'responseText contains the response received by the request               
            End Using

Here the data format is:

    service_id: 'YOUR_SERVICE_ID',
    template_id: 'YOUR_TEMPLATE_ID',
    user_id: 'YOUR_USER_ID'

And response is in JSON or text string.

For using Jobject you need to have newtonsoft DLL.

You can install newtonsoft you can use Nuget or download from the website here

Shakti Singh Cheema