Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Custom HTTP headers before form submit

    Hello,

    I need to set custom HTTP headers for fields like Authorization before I submit a form. Since it is not attached to any datasource I simply do a form.submit().

    How can I access the request object being sent so that I can set the HTTP headers?

    Any reply is greatly appreciated.

    Thanks,
    Hetal

    #2
    More explanation

    I am not sure if my question was clear or not. But from what I understand, form.submit has a native RPC request submission happening under the hood.

    I need to get access to this RPC request specifically so that I can set HTTP headers on it.

    How can I do this? Any ideas are greatly appreciated!!

    Thanks,
    Hetal

    Comment


      #3
      This is more of a GWT question. Try posting on the GWT forums.

      Sanjiv

      Comment


        #4
        Sorry but I dont understand why this is a GWT question.. For my entire application I have used SmartGWT. I am using a DynamicForm and doing a form.submitForm() by passing a URL whose API is shown as follows:

        Code:
        /**
             * Submits the form to the URL defined by {@link com.smartgwt.client.widgets.form.DynamicForm#getAction action}, 
             * identically to how a plain HTML <form> element would submit data, as either an HTTP GET or POST as specified by
             * {@link com.smartgwt.client.widgets.form.DynamicForm#getMethod method}. <P> <b>Notes:</b> <ul> <li>this is used only in
             * the very rare case that a form is used to submit data directly to a URL.  Normal server contact is through  {@link
             * com.smartgwt.client.docs.DataBoundComponentMethods 'DataBound Component Methods'}.</li> <li>For this method to reliably
             * include values for every field in the grid,       {@link com.smartgwt.client.widgets.form.DynamicForm#getCanSubmit
             * canSubmit} must be set to <code>true</code></li> <li>To submit values for fields that do not have an editor, use {@link
             * com.smartgwt.client.widgets.form.fields.HiddenItem}  with a {@link
             * com.smartgwt.client.widgets.form.fields.FormItem#getDefaultValue defaultValue} set.  This is analagous to &lt;input
             * type="hidden"&gt; in HTML forms. </ul>
             */
            public native void submitForm() /*-{
                var self = this.@com.smartgwt.client.widgets.BaseWidget::getOrCreateJsObj()();
                self.submitForm();
            }-*/;
        If you could give me details on how I can find what happens in Javascript code of self.submitForm() I could look into that code and see if it gives me access to the request object and change the HTTP headers as I want.

        Hoping to understand this...

        Thanks,
        Hetal.

        Comment


          #5
          Did you solve this problem? I am running into the same problem.

          Comment


            #6
            Actually, nevermind. The problem is ofcourse you can't add request headers without using the XMLRequest object. I will solve this with using a request parameter.

            Comment


              #7
              SmartGWT v2.5
              Browser Version:
              - IE 8 Onwards
              - Firefox 7 onwards
              - Google Chrome v 15 onwards

              This is for anyone who faces the same problem as I did while trying to modify headers.

              yes, it is possible to modify headers before sending them out. You will need to start by Subclassing com.smartgwt.client.data.DataSource or any of it's subclass which is suitable for you e.g., RestDataSource. I subclassed RestDataSource as following and modified headers. It all works:

              Code:
              public class CustomRestDS extends RestDataSource {
              	
              	public CustomRestDS(){
                      super();
                  }
              
                  public CustomRestDS(JavaScriptObject jsObj){
                      super(jsObj);
                  }
              	
              
              	@Override
              	protected Object transformRequest(DSRequest dsRequest) {
              		
              		//trying to set the authentication via HTTPHeaders
                      Map<String, String> httpHeadersMap = new HashMap<String, String>();
                      
                      //Base64 encoded username:password pair (mgahir:sunshine56)
                      httpHeadersMap.put("Authorization", "Basic bWdhaGlyOnN1bnNoaW5lNTY=");
                      
                      dsRequest.setHttpHeaders(httpHeadersMap);
              		
              	return super.transformRequest(dsRequest);
              	}
              }

              Comment

              Working...
              X