My Products
Help

How to read recently created object ID from Visma.Net API Response Headers in C# ?

07-02-2020 10:57 (Updated 07-02-2020)
  • 0 Replies
  • 0 kudos
  • 2112 Views

 

You can get a recently created object ID by looking into the Response Headers{Location} of the HTTP Response Headers.

HttpResponse.GetResponseHeader("location");

This is one of the Response Headers (LOCATION) that API returns after each successful POST Operation which contains created object ID.

 public string readLocation(string json)
            {
                string[] headers = new string[2];
                string strResponse = string.Empty;
                string location = string.Empty;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(endPoint);
               
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = httpVerb.POST.ToString();
                httpWebRequest.Headers.Add("ipp-company-id", "<CompanyID>");
                httpWebRequest.Headers.Add("ipp-application-type", "Visma.net Financials");
                httpWebRequest.Headers.Add("Authorization", "<token>");

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    headers[0] = httpResponse.GetResponseHeader("location");  
                    headers[1] = httpResponse.GetResponseHeader("date");

                    strResponse = ("ID:  "+headers[0].Substring(headers[0].LastIndexOf('/') + 1)+" // Date:  "+headers[1]);   //Seperate Resource URL - Created Object ID 
               
                }
                return strResponse ;
            }
        }

Disclaimer
The sample code on Visma Community > Visma.Net API is provided “AS IS” and any express or implied warranties, including the implied warranties of merchantability and fitness for a particular purpose, are disclaimed. In no event shall Visma or contributors be liable for any direct, indirect, incidental, special, exemplary or consequential damages.

Contributors