0

The code snippets below come from a server serving a get request

Server snippets

1)

  io.serve(handler, InternetAddress.LOOPBACK_IP_V4, 8080).then((server) {
    print('Listening on port 8080');
  }).catchError((error) => print(error));

2)

Router routes = new Router()
                      ..get('/newest', handler.handleNewest)
                      ..get('/anonymous', handler.handleAnonymousGetRequest)
                      ..post('/anonymous', handler.handleAnonymousPostRequest);

3)

shelf.Response handleNewest(shelf.Request request){
  print('got request for newest');
  return new shelf.Response.ok('sample content later fetched form db');
}

On a click event I also run this code in a client app.

void makeRequestNewest() {
  String path = 'http://127.0.0.1:8080/newest';
  HttpRequest.getString(path)
    .then((String newestContent){
    post_list.appendText(newestContent);
  })
  .catchError((Error error){
    post_list.appendText('an error occurred: ' + error.toString());
  });

unfortunately the getString is not successful. The catchError is run and results in an error occurred: Instance of '_XMLHttpRequestProgressEvent'. Also in the ToolsOutput command line of the Dart Editor I get [web] GET /newest => Could not find asset linkShepherdClient|web/newest.. This is correct, because that directory does not exist. I thought I can use the directory as a parameter to run the correct handler, and return the correct value in the .ok(), but it doesn't seem so. Does this mean I can not fetch the data from any source using the GET request, because it will always try to actually get it from this directory on the server?

Edit: code example using http package. (not working)

import 'package:http/http.dart' as http;
import 'package:http/browser_client.dart';

void makeRequestNewest() {
  String path = 'http://127.0.0.1:8080/newest';
  var client = new BrowserClient();
  client.get(path)
    .then((response){
    post_list.appendText('response status: ${response.statusCode}');
    post_list.appendText('Response body: ${response.body}');
  });
}

An answer using additional packages like the http package is also very welcome.

Lukasz
  • 2,257
  • 3
  • 26
  • 44
  • I don't really understand the question. A request isn't automatically mapped to a directory, this is what shelf_static is for. shelf_static maps the request to a file. If you have an Url path (route) which is not configured to be handled by shelf_static then you can return any content you want. – Günter Zöchbauer May 17 '15 at 18:13
  • I was confused by the `[web] GET /newest => Could not find asset linkShepherdClient|web/newest.` message. Why otherwise wouldn't this `appendText` "sample content later fetched form db". – Lukasz May 17 '15 at 18:23
  • 1
    `[web] GET /newest => Could not find asset linkShepherdClient|web/newest.` sounds like a pub serve message. Are you sure your request is actually getting to the server? Do you see `got request for newest` printed. Have you tried running `curl http://127.0.0.1:8080/newest` from the command line? Also worth adding a line `printRoutes(routes)` after you create your routes – Anders May 17 '15 at 20:42
  • @Anders. This is what happened. I launched the client. Closed the browser window. The socket was still reserved. Then I tried to launch the server.Obviously the server couldn't start listening on the reserved socket and I didn't check it since the server worked fine before. Unfortunately the value appended by the `makeReqeustNewest()` function still isn't `sample content later fetched from db`, but the `an error occurred: Instance of '_XMLHttpRequestProgressEvent'`. That's why I leave the question open. – Lukasz May 17 '15 at 21:22
  • @Anders If you are interested in taking a look at the whole code check out https://github.com/LukaszTrojanowski/linkShepherdClient and https://github.com/LukaszTrojanowski/linkShepherdServer – Lukasz May 17 '15 at 21:29
  • I can't see anything obviously wrong from the code. I've you have established that the server is working as expected (e.g. via curl) then it must be something odd with the client. I have never used HttpRequest directly, but rather always via the http package. So can't help much with that – Anders May 17 '15 at 22:01
  • @Anders I tried to apply the idea of using the http package but unfortunately unsuccessfully. Do you maybe have a github account where I could take a look at some of your code? – Lukasz May 17 '15 at 22:35

1 Answers1

0

I figured it out. I tried to access resources from a different domain (localhost:8080 and not localhost:8080/newest). This requres setting CORS header in the shelf.response. This is done for example like that

shelf.Response.ok('Message returned by handleNewest later fetched by db', headers: CORSHeader);

where CORSHeader is set to

const Map<String, String> CORSHeader = const {'Access-Control-Allow-Origin': '*'};

If you want to have access to the content from any domain. You can also replace the star with the domain you want to restrict the access to.

Lukasz
  • 2,257
  • 3
  • 26
  • 44