1

I was search this article Consuming Drupal RestApi with c# And http://tylerfrankenstein.com/code/drupal-services-csrf-token-firefox-poster

I has question in the cookie and token. I have test in poster with firefox and successful on post the created article .Tamper Data has the request header.

tamper data

nid: "129342" uri: http://www.tsghy.com.cn/services/node/129342

Postman created the post code

                var client = new RestClient("http://www.tsghy.com.cn/services/node");
            var request = new RestRequest(Method.POST);
            request.AddHeader("postman-token", "5c28c9d6-d640-a4f0-a549-b6018e62907d");
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("x-csrf-token", "s0Z17LT7neX_K6grHgoJCUPR6VcL2QxRlNLmbRWeExE");
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddParameter("application/x-www-form-urlencoded", "type=article&title=test%201", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

I will crazy,

And replace to the C# ,i has Access denied for user anonymous.this my code with below : First I login the Drupal with Rest

private login_user2 loginAsync2(string username, string password)
    {
        try
        {
            RestClient client = new RestClient(base_url2);
            var request = new RestRequest("user/login.json", Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            client.Authenticator = new SimpleAuthenticator("username",username,"password",password);
            var restResponse = client.Execute(request);
            var content = restResponse.Content;
            if (restResponse.StatusCode==System.Net.HttpStatusCode.OK)
            {
                login_user2 loginuser = JsonConvert.DeserializeObject<login_user2>(content.ToString());                    
                request = new RestRequest("session/token", Method.GET);
                restResponse = client.Execute(request);
                loginuser.session_token = restResponse.Content.ToString();
                return loginuser;
            }
            else {
                return null;
            }
        }
        catch (Exception ex) { throw ex; }
    }

I have question at the login/user->token and session/token ,which the difference?

Second,Post the create data:

        RestClient client = new RestClient(base_url2);
        var request = new RestRequest("node", Method.POST);
        request.AddHeader("cache-control", "no-cache"); 
        request.AddHeader("content-type", "application/json; charset=UTF-8");
        request.AddHeader("Accept", "application/json");
        request.AddHeader("cookie", "Drupal.toolbar.collapsed=0; "+current_user2.session_name+"="+current_user2.sessid+"; has_js=1");
        request.AddHeader("x-csrf-token",current_user2.session_token);
        request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
        request.AddParameter("application/json", myjobject, ParameterType.RequestBody);

        var queryresult = client.Execute(request);

1 Answer 1

0

yes,I found the key,restful has bug at the request.addheader(cookie,cookie); change to :request.AddParameter(session_name,session_id,parametertype.cookie);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.