making http requests (fetch) from the server

13 views
Skip to first unread message

pmanacas

unread,
May 24, 2011, 12:42:13 PM5/24/11
to akshell
Hi,

Having JS in the server is awesome, but sometimes i get a bit
confused.
Imagine I wanted to make an http request from the server. (like to
query a RESTfull API for example)
How would I do it?

Oleg Podsechin

unread,
May 24, 2011, 1:41:07 PM5/24/11
to aks...@googlegroups.com
You can use a library that I wrote: https://github.com/olegp/ak-httpclient

It is currently available at olegp/httpclient:1.0 (add that to
manifest.json or via the menus), although Anton is about to move it to
ak/httpclient:1.0 and will probably add its description to the docs.
Anton, please update this thread when you've done it.

More info here: http://wiki.commonjs.org/wiki/HTTP_Client/A
Usage example here:
https://github.com/isaacs/narwhal-playground/blob/master/http-client.js
(note the different module name in require)

Nice thing is that unlike within the browser, the response is
synchronous, so you get the results returned to you. Also, you're not
constrained by the same origin policy, so you can make a request to
any URL (except HTTPS, support for which is on its way).

Oleg

> --
> You received this message because you are subscribed to the Google Groups "akshell" group.
> To post to this group, send email to aks...@googlegroups.com.
> To unsubscribe from this group, send email to akshell+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/akshell?hl=en.
>
>

Pedro Manaças

unread,
May 24, 2011, 1:55:20 PM5/24/11
to aks...@googlegroups.com
perfect thanks! :-)

Dash

unread,
May 24, 2011, 11:37:12 PM5/24/11
to aks...@googlegroups.com
Oleg what happens then if the request times out or something?

Pedro Manaças

unread,
May 25, 2011, 7:43:08 AM5/25/11
to aks...@googlegroups.com
ok, I'm hitting a few walls...
sorry if I seem a js noob but im used to doing this in python instead.

I need to execute a POST request to an url
and the body needs to be valid JSON

I can't seem to figure out how to set the body correctly
my code looks like this:

var HttpClient = require('httpclient').HttpClient;            
      var result = HttpClient({
              method: "POST",
              url : "http://mytargeturl.com",
              body : [{"message": "Hello world"}]
          })
          .setHeader("Content-Type", "application/x-www-form-urlencoded")
          .connect().read();

Can you help me with the correct syntax to set the body to a valid JSON string?

thanks a lot
Pedro

On 25 May 2011 04:37, Dash <dxgri...@gmail.com> wrote:
Oleg what happens then if the request times out or something?

--

Oleg Podsechin

unread,
May 25, 2011, 8:13:55 AM5/25/11
to aks...@googlegroups.com

Body is an array of strings, so you should use JSON.stringify to turn your object into a string.

On May 25, 2011 5:43 PM, "Pedro Manaças" <pedrom...@gmail.com> wrote:
> ok, I'm hitting a few walls...
> sorry if I seem a js noob but im used to doing this in python instead.
>
> I need to execute a POST request to an url
> and the body needs to be valid JSON
>
> *I can't seem to figure out how to set the body correctly*

> my code looks like this:
>
> var HttpClient = require('httpclient').HttpClient;
> var result = HttpClient({
> method: "POST",
> url : "http://mytargeturl.com",
> body : [{"message": "Hello world"}]
> })
> .setHeader("Content-Type", "application/x-www-form-urlencoded")
> .connect().read();
>
> *Can you help me with the correct syntax to set the body to a valid JSON
> string?*
> *
> *

Pedro Manaças

unread,
May 25, 2011, 8:47:35 AM5/25/11
to aks...@googlegroups.com
yes, I understand i can use JSON.stingify
at the moment just for test purposes im hardcoding the list of stings like in my example code (an array with 
and im getting the error:

The script is to make  post request to an external api that returns a response. Im just hardcoding everything to test it out.
ValueError: data and post cannot be specified together
    at new <anonymous> (native init.js:15:13)
    at Object.ValueError (native init.js:12:26)
    at Object.requestHost (akshell/ak:0.3:rest.js:510:16)
    at Object.read (olegp/httpclient:1.0:index.js:123:23)
    at [object Object].get (main.js:21:22)
    at [object Object].handle (akshell/ak:0.3:rest.js:132:29)
    at akshell/ak:0.3:rest.js:211:27
    at akshell/ak:0.3:rest.js:471:14
    at akshell/ak:0.3:rest.js:451:14
    at akshell/ak:0.3:rest.js:439:14
    at akshell/ak:0.3:rest.js:392:14
    at akshell/ak:0.3:rest.js:249:14
    at Object.main (akshell/ak:0.3:rest.js:237:20)
    at Object.app (akshell/ak:0.3:rest.js:188:41)
    at default:jsgi.js:91:39

Here's the full code:
require('ak').setup();
require('httpclient');

// The index page handler
var IndexHandler = Handler.subclass(
  {
    
    get: function (request) {
      var HttpClient = require('httpclient').HttpClient;            
      var result = HttpClient({
              method: "POST",
              url : "http://myurl.com",
              body : ["{'message': 'Hello world'}"]
          })
          .setHeader("Content-Type", "application/x-www-form-urlencoded")
          .connect().read();
  
  var list=[];
  headers = result.headers;
  
  for (field in headers) {
    list.push(headers[field]);
    }

    return render(
          'index.html',
        {
          header: result.status + list
        });
    }
  });


// The URL -> handler mapping
exports.root = new URLMap(
  IndexHandler, 'index');

Oleg Podsechin

unread,
May 25, 2011, 9:09:36 AM5/25/11
to aks...@googlegroups.com
It was a problem in my code, this has now been fixed and the changes
are already available to you. Just reload the page.

https://github.com/olegp/ak-httpclient/commit/2c66a75a3cd06aaae676ec6deb1633cd8c51e08d

Pedro Manaças

unread,
May 25, 2011, 9:23:27 AM5/25/11
to aks...@googlegroups.com
Awesome, got it working :-)
(this really highlights the benefit of these live importable modules)

thanks for your speedy reply and help :-)
Reply all
Reply to author
Forward
0 new messages