Skip to Content
Apr 26, 2017 at 02:49 PM

Getting POST to Hana xsjs Service to work from C#

478 Views Last edit Apr 26, 2017 at 02:50 PM 2 rev


I am trying do do a post from C# using the RestSharp library.

I get a x-csrf-token form the service using a HEAD HTTP call. This works just fine.

I the pass it to POST command but keep on getting a 403 Forbidden response.

i made sure that I use the same RestSharp / RestRequest instance so that I do not end up with session issues.

Can anyone see the error i am making ?

using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TiCC3200STK_Reader
    class HcpServices
        // ----------------------------------------------------------------
        // Assuming that we are working with the DeviceServices - 
        //      getting x-csrf token
        // -----------------------------------------------------------------
        private RestClient client;
        private RestRequest request;

        public HcpServices()
            client = new RestClient("");
            request = new RestRequest();

        public IList<Parameter> GetCsrfToken()
            request.Method = Method.HEAD;
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("x-csrf-token", "fetch");
            request.AddHeader("authorization", "Basic XXXXXXXXXXXXXXXXXXX");
            IRestResponse response = client.Execute(request);

            return response.Headers;

        public string RegisterDevice(string projId, string msgTyp, string deviId)
            string retVal = "";

            foreach (var obj in GetCsrfToken())
                if (obj.Name.StartsWith("x-"))
                    // Remove the "x-csrf-token : fetch parameter
                    // Add the token from the response to the HEAD call
                    request.AddParameter(obj.Name, obj.Value, obj.Type);

            request.AddParameter("application/json", "{\"ProjectId\":\"634e97e9-1721-406d-b4e3-6849012e530c\",\"MsgTypeId\":\"9429a8bc-d487-433a-a9f5-2db1d9556d19\",\"DeviceId\":\"e8e69a84-9c4f-4c3c-8bda-ad07771fdbd2\"}", ParameterType.RequestBody);
            request.Method = Method.POST;
            IRestResponse response = client.Execute(request);
            return retVal;

Any help will be greatly appreciated
Here is the request header before making the POST call




untitled.png (37.0 kB)