HTTP Plugin: How to do a POST method?

Questions and answers on developing, deploying and using plugins and JavaBeans

HTTP Plugin: How to do a POST method?

Postby palacio » Sun Nov 15, 2015 3:17 am

Hi,

I am trying to use the http plugin and I am having a hard time to make a POST method
i was using these codes :

var v_poster = plugins.http.getPoster('i_have_a_api_url_here');
v_poster.addHeader('Content-type','text/json; charset=UTF-8');
v_poster.addParameter('name','here');
v_poster.doPost();
var v_result = v_poster.getPageData();

Am I missing something ? because I am always getting the error "Your request did not include an API key." but I tried putting my
api key inside the url and the addParameter both of them gave me the same error.

Thanks guys in advance,
palacio
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm

Re: HTTP Plugin: How to do a POST method?

Postby Gordon McLean » Sun Nov 15, 2015 11:33 am

Hi Palacio

I have not tested the following but it should work, notes are in the example:


var sResults = ' ' //container for the results

//Create the client
var client = plugins.http.createNewHttpClient();

//add parameters to the client
var poster = client.createPostRequest(https://uk1.api.mailchimp.com/2.0/'); //uk1 or whatever is the string at the end of your api key myapikey-uk1
poster.addParameter('apikey', 'Your API Key HERE');
poster.addParameter('id', 'some id');

poster.setCharset('UTF-8'); //optional may help

//Execute the post and get the results
sResults = poster.executeRequest().getStatusCode();

//See if anything happened in the console
application.output(sResults)

//returns the results
return sResults
Gordon McLean
Clickdigital.com
Gordon McLean
 
Posts: 253
Joined: Wed Aug 03, 2005 12:24 pm
Location: UK

Re: HTTP Plugin: How to do a POST method?

Postby palacio » Sun Nov 15, 2015 4:40 pm

Gordon McLean wrote:Hi Palacio

I have not tested the following but it should work, notes are in the example:


var sResults = ' ' //container for the results

//Create the client
var client = plugins.http.createNewHttpClient();

//add parameters to the client
var poster = client.createPostRequest(https://uk1.api.mailchimp.com/2.0/'); //uk1 or whatever is the string at the end of your api key myapikey-uk1
poster.addParameter('apikey', 'Your API Key HERE');
poster.addParameter('id', 'some id');

poster.setCharset('UTF-8'); //optional may help

//Execute the post and get the results
sResults = poster.executeRequest().getStatusCode();

//See if anything happened in the console
application.output(sResults)

//returns the results
return sResults


Thank you Mr. Gordon, I will try your the codes that you wrote, but will there errors if I will use the codes in Servoy 5.2?

palacio
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm

Re: HTTP Plugin: How to do a POST method?

Postby ROCLASI » Sun Nov 15, 2015 8:07 pm

Hi palacio,

You maybe want to take a look at the Velocity plugin. This has next to it's Reporting and Webclient/services functionality also Velocity Services. Which is functionality to consume external web services like the MailChimp REST API.
It should work fine with Servoy 5.2.
See the wiki for more info.

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: HTTP Plugin: How to do a POST method?

Postby palacio » Mon Nov 16, 2015 11:14 am

ROCLASI wrote:Hi palacio,

You maybe want to take a look at the Velocity plugin. This has next to it's Reporting and Webclient/services functionality also Velocity Services. Which is functionality to consume external web services like the MailChimp REST API.
It should work fine with Servoy 5.2.
See the wiki for more info.

Hope this helps.


Hi Mr. Ivens

Thank you for the codes and for the advise I will try the velocity plugin and hope that it will help me finish the project.

palacio
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm

Re: HTTP Plugin: How to do a POST method?

Postby Gordon McLean » Mon Nov 16, 2015 11:26 am

Thank you Mr. Gordon, I will try your the codes that you wrote, but will there errors if I will use the codes in Servoy 5.2?

palacio


Your welcome, I did not test the code in 5.2 but I do not see a reason why it should fail. I had a very similar requirement here using 5.2:

viewtopic.php?f=15&t=18141

I suspect the @roclasi solution will also work as will the Servoy REST plugin, so all three options are possible

Gordon
Gordon McLean
Clickdigital.com
Gordon McLean
 
Posts: 253
Joined: Wed Aug 03, 2005 12:24 pm
Location: UK

Re: HTTP Plugin: How to do a POST method?

Postby palacio » Wed Nov 18, 2015 3:58 pm

var urlpost = 'https://usXX.api.mailchimp.com/3.0/lists/';
var sessionname = 'mailchimptest'
var post = plugins.http.getPoster(urlpost, sessionname);
post.addHeader('content-type','application/json; charset=utf-8');
post.setCharset('utf-8');
post.addParameter("apikey","xxxxxx")
post.addParameter("email_address","gardin@databridge.it2");
post.addParameter("status","subscribed");
post.doPost();
var data = post.getPageData();
application.output(data);

I'm trying to run these codes but the result is always:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"API Key Missing","status":401,"detail":"Your request did not include an API key.","instance":""}
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm

Re: HTTP Plugin: How to do a POST method?

Postby ROCLASI » Wed Nov 18, 2015 8:10 pm

Hi Palacio,

First of all, don't put your API key on this forum. I have moderator rights so I edited your post to 'x' them out.
I suggest you get a new api key with mail chimp to be safe.

And to answer your question, api-keys usually go in the header.
So instead of post.addParameter("apikey","xxxxxx") you use post.addHeader("apikey","xxxxxx")

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: HTTP Plugin: How to do a POST method?

Postby palacio » Sat Nov 21, 2015 1:11 am

ROCLASI wrote:Hi Palacio,

First of all, don't put your API key on this forum. I have moderator rights so I edited your post to 'x' them out.
I suggest you get a new api key with mail chimp to be safe.

And to answer your question, api-keys usually go in the header.
So instead of post.addParameter("apikey","xxxxxx") you use post.addHeader("apikey","xxxxxx")

Hope this helps.


Sir I'm sorry for posting some of my apikey, I tried the codes that you gave me but it still give me an error.

var v_test_post = plugins.http.getPoster("https://UK.api.mailchimp.com/3.0/","test");
v_test_post.addHeader("apikey","XXAPIKEY");
v_test_post.addParameter("name","adele");
v_test_post.doPost();
var v_result = v_test_post.getPageData();

result: Your request did not include an API key.
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm

Re: HTTP Plugin: How to do a POST method?

Postby ROCLASI » Sat Nov 21, 2015 1:15 pm

Hi Palacio,

Okay, now you made me look at their API page ;)
It seems they work with either Basic Authentication or oAuth2.
Basic auth is the easiest and you could use the addHeader function but that also requires some Base64 encoding. Instead use the build in function of the http plugin like so:
Code: Select all
// Servoy 5.2. code, for later Servoy versions you need to rewrite this
var v_test_post = plugins.http.getPoster("https://UK.api.mailchimp.com/3.0/","test");
v_test_post.addParameter("name","adele");
var v_httpResultCode = v_test_post.doPost("", "XXAPIKEY"); // <-- this does it
var v_result = v_test_post.getPageData();


Hope this helps
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: HTTP Plugin: How to do a POST method?

Postby palacio » Sun Nov 22, 2015 1:45 am

ROCLASI wrote:Hi Palacio,

Okay, now you made me look at their API page ;)
It seems they work with either Basic Authentication or oAuth2.
Basic auth is the easiest and you could use the addHeader function but that also requires some Base64 encoding. Instead use the build in function of the http plugin like so:
Code: Select all
// Servoy 5.2. code, for later Servoy versions you need to rewrite this
var v_test_post = plugins.http.getPoster("https://UK.api.mailchimp.com/3.0/","test");
v_test_post.addParameter("name","adele");
var v_httpResultCode = v_test_post.doPost("", "XXAPIKEY"); // <-- this does it
var v_result = v_test_post.getPageData();


Hope this helps


Hi Mr. Ivens,

I tried the codes that you wrote but it still gives the same error of " Your request did not include an API key. " I tried every possible :
a. var v_httpResultCode = v_test_post.doPost("", "XXAPIKEY");
b. var v_httpResultCode = v_test_post.doPost("APIKEY", "XXXXX");
c. var v_httpResultCode = v_test_post.doPost("APIKEY:", "XXXXX");
d. var v_httpResultCode = v_test_post.doPost("", "APIKEY:XXXXX");
e. var v_httpResultCode = v_test_post.doPost("?APIKEY:", "XXXXX");
f. var v_httpResultCode = v_test_post.doPost("", "APIKEY=XXXXX");

palacio
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm

Re: HTTP Plugin: How to do a POST method?

Postby ROCLASI » Sun Nov 22, 2015 2:23 am

Hi Palacio,

I don't know if you use the term APIKEY in your api key string....according to the documentation you only use the actual key in the authentication.
So v_test_post.doPost("", "<your actual apikey>") should do it.
Or perhaps the documentation needs to be taken literal and the username is indeed 'anystring' like so:
v_test_post.doPost("anystring", "<your actual apikey>");

Please replace the '<your actual apikey>' with the api KEY.

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: HTTP Plugin: How to do a POST method?

Postby palacio » Sun Nov 22, 2015 4:33 pm

ROCLASI wrote:Hi Palacio,

I don't know if you use the term APIKEY in your api key string....according to the documentation you only use the actual key in the authentication.
So v_test_post.doPost("", "<your actual apikey>") should do it.
Or perhaps the documentation needs to be taken literal and the username is indeed 'anystring' like so:
v_test_post.doPost("anystring", "<your actual apikey>");

Please replace the '<your actual apikey>' with the api KEY.

Hope this helps.


Mr. Ivens ,

Yes it was right to put the apikey to the .doPost() method but not for MailChimp.
I tried MailGun and used its api key and I tried this codes:
Code: Select all
   var v_mail_gun = plugins.http.getPoster(" https://api.mailgun.net/v3/DOMAIN/messages", "testMAIL");
      v_mail_gun.addParameter("from","from@mail.com");
      v_mail_gun.addParameter("to","to@mail.com");
      v_mail_gun.addParameter("subject","Hello");
      v_mail_gun.addParameter("text","Tryme");
      v_mail_gun.doPost("api","key-XXXX"); // <-- it works for MailGun
   var v_result_mail = v_mail_gun.getPageData();
   application.output(v_result_mail);


Result:
"message": "Queued. Thank you."

Maybe I need more time reading the MailChimp Documents if I still want to use it.

palacio
palacio
 
Posts: 47
Joined: Tue Aug 18, 2015 12:29 pm


Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 4 guests