Question

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;
    }

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top