Skip to Content
0

Getting POST to Hana xsjs Service to work from C#

Apr 26, 2017 at 02:49 PM

116

avatar image

Hi,

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("https://iojxxxxxxx.us1.hana.ondemand.com/xxxxxxxxxxxxx/DeviceService/SetupDevice.xsjs?mode=configure");
            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
                    request.Parameters.RemoveAt(1);
                    // 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

Thanks

Andre

untitled.png (37.0 kB)
c# | post | 403
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Andre Van Staden Apr 28, 2017 at 11:52 AM
0

As always I end up answering my own questions. Remeber to pass the cookies along too ...

Share
10 |10000 characters needed characters left characters exceeded