1. #1
    Sencha User
    Join Date
    Oct 2013
    Posts
    7
    Vote Rating
    0
    giamak is on a distinguished road

      0  

    Default HttpProxy Post example

    HttpProxy Post example


    I need an example of the use of POST in HttpProxy.
    I want to send an object from the client to the server.


    I want to complete this code:

    Code:
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
    					path);
    HttpProxy<PagingLoadConfig> proxy = new HttpProxy<PagingLoadConfig>(
    					builder);
    proxy.setWriter(new PagingLoadUrlEncoder());
    
    
    ///////
    
    public class PagingLoadUrlEncoder implements
    		DataWriter<PagingLoadConfig, String> {
          
           public String write(PagingLoadConfig config) {
                   MyObjectClassToSend obj;
                   String str;
    
                   ????????????????
    
                   return str;
    
          }
    }
    Ideas?

    Thanks in Advance

  2. #2
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,717
    Vote Rating
    88
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    I want to send an object from the client to the server.
    How do you want it to be sent? As JSON? As a url-encoded form? Do you have a custom format you want to use?

    Also what is the PagingLoadUrlEncoder class? That doesn't appear to be part of GXT. We have built-in support for Json, XML, and UrlEncoded contents through our built-in Writers, all of which use autobeans to encode the data being sent, the same autobeans which are used to read data when it comes back from the server.

  3. #3
    Sencha User
    Join Date
    Oct 2013
    Posts
    7
    Vote Rating
    0
    giamak is on a distinguished road

      0  

    Default Thanks

    Thanks


    Thanks for the reply Colin,
    I would like to add information to the request sent to the server,
    normally send these informations using the UrlEncoded.
    When I do GET, I am writing for example:

    Code:
    public class UrlBuilder {
    
    
        protected StringBuilder url = new StringBuilder();
        protected boolean first = true;
    
    
        public void clear()
        {
            url = new StringBuilder();
            first = true;
        }
    
    
        public void addParameter(String name, String value)
        {
            if (value!=null) {
                if (first) first = false;
                else url.append('&');
    
    
                url.append(URL.encodeQueryString(name));
                url.append('=');
                url.append(URL.encodeQueryString(value));
            }
        }
    
    
        public String toString()
        {
            return url.toString();
        }
    
    
    }
    
    public class PagingLoadUrlEncoder implements
        DataWriter<PagingLoadConfig, String> {
    
    
        protected UrlBuilder urlBuilder = new UrlBuilder();
        
        public String write(PagingLoadConfig config) {
            
            urlBuilder.clear();
    
            urlBuilder.addParameter(ServletParameters.OFFSET,
                    String.valueOf(config.getOffset()));
            urlBuilder.addParameter(ServletParameters.LIMIT,
                    String.valueOf(config.getLimit()));
            if(config.getSortInfo()!=null && !config.getSortInfo().isEmpty()){
                SortInfo info = config.getSortInfo().get(0);
                urlBuilder.addParameter(ServletParameters.SORTFIELD,
                        info.getSortField());
                urlBuilder.addParameter(ServletParameters.SORTDIR, (info
                        .getSortDir() == null) ? null : info.getSortDir()
                        .toString());
            }
            
            return urlBuilder.toString();
    
    
        }
    Now if I want to send an object and want to read this server-side object, how should I use the system you speak of? If I want use JSON?

  4. #4
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,717
    Vote Rating
    88
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    Since you are writing for a custom format for your GET url (and not using our built-in url encoding writer), I'm assuming you also want a custom for your POST data? What encoding do you plan on using, JSON or url-encoded? If you want a custom format beside the built-in JsonWriter or UrlEncodingWriter, then all you need to do is supply the writer like you are doing and set the request method to POST. Internally HttpProxy will decide that since you are using POST it should append the data to the body of the request instead of to the end of the url:
    Code:
      /**
       * Decides if the config data should be written to the body or to the querystring. Defaults to
       * using the body in the case of {@code POST} or {@code PUT} requests, and the querystring for
       * all other requests. Can be overridden to change this behavior.
       * 
       * @return true if the outgoing request should write the config data to the body instead of using
       * writing to the querystring
       */
      protected boolean shouldUseBody() {
        return builder.getHTTPMethod().equals("POST") || builder.getHTTPMethod().equals("PUT");
      }

  5. #5
    Sencha User
    Join Date
    Oct 2013
    Posts
    7
    Vote Rating
    0
    giamak is on a distinguished road

      0  

    Default Thanks

    Thanks


    I think I have solved my problem,
    I used POST and send the data in the request body as JSON.

    Thanks Colin

Thread Participants: 1

Tags for this Thread