3

I am building an Android app that talks to Services module using REST endpoint. When I post any content without setting the "has_js=1" cookie to the REST endpoint in JSON format, my content is never accepted by Drupal or Service module.

What is the use of has_js cookie and why do I need to send along with my POST requests?

Update:

Following is my code to post the article content type. If I do not add the has_js=1 cookie, article won't be posted to Drupal site.

 protected Integer doInBackground(String... params) {

        //read session_name and session_id from passed parameters
        String session_name=params[0];
        String session_id=params[1];


        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://ec2-54-***-**-***.us-west-2.compute.amazonaws.com/cmac/rest/node");



        try {

            //get title and body UI elements
            TextView txtTitle = (TextView) findViewById(R.id.editTitle);
            TextView txtBody = (TextView) findViewById(R.id.editBody);

            //extract text from UI elements and remove extra spaces
            String title=txtTitle.getText().toString().trim();
            String body=txtBody.getText().toString().trim();

            //create JSON object to pass to Services endpoint
            JSONObject json= new JSONObject(" { \"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}");

            StringEntity se = new StringEntity( json.toString());
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httppost.setEntity(se);


            BasicHttpContext mHttpContext = new BasicHttpContext();
            CookieStore mCookieStore      = new BasicCookieStore();

            //create the session cookie
            BasicClientCookie cookie = new BasicClientCookie(session_name, session_id);
            cookie.setVersion(0);
            cookie.setDomain(".ec2-54-***-**-***.us-west-2.compute.amazonaws.com");
            cookie.setPath("/");
            mCookieStore.addCookie(cookie);
            cookie = new BasicClientCookie("has_js", "1");
            mCookieStore.addCookie(cookie);
            mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

            httpclient.execute(httppost,mHttpContext);

            return 0;

        }catch (Exception e) {
            Log.v("Error adding article",e.getMessage());
        }

        return 0;
    }
  • I use Services all the time and I've never passed a has_js parameter or cookie value to any endpoint/resource (and never received any error about it). Presumably the specific resource you're consuming requires it for some reason. What resource are you actually using? – Clive Jul 1 '13 at 9:19
  • @Clive I have added the code to show what resource I am trying to use. – Ajinkya Kulkarni Jul 1 '13 at 18:52
1

The has_js cookie has been used by the Batch API. Without this cookie, or when JS is disabled in the client browser, the batch API will give you a WSOD.

See $_COOKIE['has_js'] must die.

If your service endpoint is dealing with the Batch API internally, then you will have this problem.

  • On the link that you posted, it is mentioned that batches are usually invoked by form submission. Since I am trying to post the article content type, I am guessing that form submission is invoked and my request is denied. – Ajinkya Kulkarni Jul 1 '13 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.