1

I'm working with the Harvest API, a pretty standard web service API, and my curl requests are working just fine while my Dart HttpClient requests are not. Here is my curl request (with sensitive information disguised, of course):

curl -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -u "address@domain.com:password" \
  https://my_domain.harvestapp.com/account/who_am_i

UPDATE --- The following code now works:

HttpClient client = new HttpClient();
client.addCredentials(
  Uri.parse('https://my_domain.harvestapp.com/account/who_am_i'),
  'realm',
  new HttpClientBasicCredentials('address@domain.com', 'password')
);
client.getUrl(Uri.parse('https://my_domain.harvestapp.com/account/who_am_i'))
  .then((HttpClientRequest req) {
    req.headers
      ..add(HttpHeaders.ACCEPT, 'application/json')
      ..add(HttpHeaders.CONTENT_TYPE, 'application/json');

    return req.close();
  })
  .then((HttpClientResponse res) {
    print(res);
    client.close();
  });
}

Obviously I would like to do more than simply print the response, but no matter what, the res object ends up being null, which means that the request is failing in some respect. Does anything seem awry or amiss? I'm at a loss for now.

lucperkins
  • 768
  • 1
  • 7
  • 15
  • 1
    Try calling `addCredentials` before calling `getUrl`. Also try `return req.close()`. – MarioP Feb 07 '14 at 09:54
  • @MarioP That works in the sense that it will return a non-null `res` variable and a message from the server: `You must be authenticated with Harvest to complete this request.` But somehow the authentication itself doesn't seem to be going through. – lucperkins Feb 07 '14 at 21:21
  • 1
    Hm. Shouldn't the parameter to `Uri.parse` also match the URL in `getUrl`? – MarioP Feb 08 '14 at 11:24
  • @MarioP You're right. That was a mistake in copying and pasting. Unfortunately, still doesn't work :/ – lucperkins Feb 08 '14 at 18:21
  • @MarioP Actually, in going back and fixing my copy/paste error, I noticed a tiny typo. The above code works just fine. Thank you! – lucperkins Feb 08 '14 at 18:30

1 Answers1

1

To summarize the changes from the original code snippet:

  • Call HttpClient.addCredentials before calling HttpClient.getUrl
  • In the .then((HttpClientRequest)) part, make sure to return req.close()
  • Make sure the URLs in addCredentials and getUrl match.
MarioP
  • 3,752
  • 1
  • 23
  • 32