HTTP Post Request with AddParameter...

Questions, tips and tricks and techniques for scripting in Servoy

HTTP Post Request with AddParameter...

Postby rainer » Tue Apr 23, 2013 5:24 pm

Servoy 6.1

I do the following Post and the addParameter is NOT sent ?
The Parameter is returned always as body.
How to fix it and get it work ??

Thank you !

//----------------------------------------------------------------

var client = plugins.http.createNewHttpClient();
var poster = client.createPostRequest('http://posttestserver.com/post.php?dump')

poster.addHeader('Host','posttestserver.com')
poster.addHeader('Content-Type', 'text/xml');
poster.addHeader('x-amazon-user-agent', 'Test-Agent');
poster.addHeader('Content-MD5','mySignature');
poster.addParameter('SignatureVersion', '2');

poster.setCharset('UTF-8');

var response = poster.executeRequest()
var pageData = response.getResponseBody();

application.output(pageData)


//----------------------------------------------------------------
rainer
 
Posts: 11
Joined: Fri Aug 17, 2012 9:58 am

Re: HTTP Post Request with AddParameter...

Postby studiomiazzo » Thu May 23, 2013 4:31 pm

the code seems to be right. the problem could be the xml returned.
what do you exactly have in your variable pageData?
User avatar
studiomiazzo
 
Posts: 124
Joined: Thu Jun 16, 2011 10:48 am
Location: Novara (IT)

Re: HTTP Post Request with AddParameter...

Postby david » Thu May 23, 2013 4:58 pm

Some example post code. Note that the addParameter is used to send the body of the request:

Code: Select all
/**
* @param {String} url Modifies base URL to go to correct UPS endpoint
* @param {String} body UPS xml string to post
* @return {Object} either err or xml data returned

* @properties={typeid:24,uuid:"F639568D-8098-4750-84FE-31805CF9D851"}
* @AllowToRunInFind
* @private
*/
function _poster(url, body) {
     
   var endpoint = _baseURL + url
   
   // construct poster
   var client = plugins.http.createNewHttpClient();
   var poster = client.createPostRequest(endpoint);
   poster.addHeader('Content-type', 'application/x-www-form-urlencoded')
   poster.addParameter(null, body) //sets the content to post
   
   // send request
   /** @type {String} */
   var responseString   = poster.executeRequest().getResponseBody()
   
   // parse response
   var response = { data : null, err : { code : null, msg : null } }
   
   // error: didn't reach endpoint
   if ( responseString.search(/<\!DOCTYPE/) != -1 ) {
      response.err.code    = "302"
      response.err.msg   = "Endpoint not there"
   }
   else {
      if ( responseString.search(/\n/) != -1) {
         // line breaks in response
         response.data = XML(responseString.substr(responseString.search(/\n/) + 1, 100000))
      }
      else {
         // no line breaks in response
         response.data = XML(responseString.substr(responseString.search(/>/) + 1, 100000))
      }
   }
   
   // error: response returns error
   if ( response.data.Response.Error.ErrorCode ) {
      response.err.code    = response.data.Response.Error.ErrorCode.toString()
      response.err.msg   = response.data.Response.Error.ErrorDescription.toString()
   }
   return response     
}


A quick search for an Amazon example: http://docs.aws.amazon.com/AWSImportExp ... quest.html. Looks like the body variable in your case should look something like this:

Code: Select all
Action=GetStatus&SignatureMethod=HmacSHA256&JobId=JOBID&AWSAccessKeyId=&SignatureVersion=2&Version=2010-06-03&Signature=%2FVfkltRBOoSUi1sWxRzN8rw%3D&Timestamp=2011-06-20T22%3A30%3A59.556Z
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.


Return to Methods

Who is online

Users browsing this forum: No registered users and 6 guests