HTTP Plugin: How to do a POST method?

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

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:
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

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.

ROCLASI:
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

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

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”:“”}

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.

ROCLASI:
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.

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:

// 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

ROCLASI:
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:

// 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

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(“”, “”) 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”, “”);

Please replace the ‘’ with the api KEY.

Hope this helps.

ROCLASI:
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(“”, “”) 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”, “”);

Please replace the ‘’ 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:

	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