0
votes

I'm trying to use curl in SAS to download data from an api. I have a Consumer key,Consumer secret,OAuth Token, & OAuth Token Secret. It appears that I can connect to the api but I get an error stating that "HTTP Transport: Couldn't determine the content length". Does anyone have any insights or thoughts? Thanks so much.

Code:

dm 'clear log';

options;

%let consumer_key =;
%let consumer_secret=; 
%let oauth_token=; 
%let oauth_token_secret=;

filename curl pipe "curl -X POST -k https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=aapl --verbose --header 
'Authorization:OAuth oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0,
oauth_signature_method=HMAC-SHA1,oauth_consumer_key={&consumer_key.},oauth_consumer_key_secret={&consumer_secret.},
oauth_token={&oauth_token.},oauth_token_secret={&oauth_token_secret.}";

data _null_;
  infile curl lrecl=32767;
  input;
  put _infile_;
run;

Log:

 dm 'clear log';

options;
636
637  %let consumer_key =;
638  %let consumer_secret=;
639  %let oauth_token=;
640  %let oauth_token_secret=;
641
642      filename curl pipe "curl -X POST -k
642! https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=aapl --verbose --header
643      'Authorization:OAuth
643! oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0,
WARNING: The quoted string currently being processed has become more than 262 characters long.
          You might have unbalanced quotation marks.
644      oauth_signature_method=HMAC-SHA1,oauth_consumer_key={&consumer_key.},oauth_consumer_ke
644! y_secret={&consumer_secret.},
645      oauth_token={&oauth_token.},oauth_token_secret={&oauth_token_secret.}";
646
647  data _null_;
648    infile curl lrecl=32767;
649    input;
650    put _infile_;
651  run;

NOTE: The infile CURL is:
      Unnamed Pipe Access Device,

      PROCESS=curl -X POST -k https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=aapl
      --verbose --header    'Authorization:OAuth
      oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0,

      oauth_signature_method=HMAC-SHA1,oauth_consumer_key={
      JXpV},oauth_consumer_key_secret={},
      oauth_token={},oauth_token_secret={},
      RECFM=V,LRECL=32767

Fault Name: HttpRequestReceiveError
Error Type: Default
Description: Http request received failed
Root Cause Code: -19013
Root Cause : HTTP Transport: Couldn't determine the content length
Binding State: CLIENT_CONNECTION_ESTABLISHED
Service: null
Endpoint: null
Operation (Client):

--_curl_--oauth_signature_method=HMAC-SHA1,oauth_consumer_key=

--_curl_--oauth_token=,oauth_token_secret=Q

Stderr output:
* Adding handle: conn: 0x1ea8850
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1ea8850) send_pipe: 1, recv_pipe: 0
   Total     Received  Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to
connect() to api.tradeking.com port 443 (#0)
*   Trying 206.132.7.9...

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to
api.tradeking.com (206.132.7.9) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Server hello (2):
{ [data not shown]
* SSLv3, TLS handshake, CERT (11):
{ [data not shown]
* SSLv3, TLS handshake, Server finished (14):
{ [data not shown]
* SSLv3, TLS handshake, Client key exchange (16):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Finished (20):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
{ [data not shown]

  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0* SSLv3, TLS
handshake, Finished (20):
{ [data not shown]
* SSL connection using RC4-MD5
* Server certificate:
*    subject: OU=Domain Control Validated; OU=COMODO SSL Wildcard; CN=*.tradeking.com
*    start date: 2013-06-09 00:00:00 GMT
*    expire date: 2016-06-08 23:59:59 GMT
*    issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO SSL CA
*    SSL certificate verify result: self signed certificate in certificate chain (19),
continuing anyway.
> POST /v1/market/ext/quotes.xml?symbols=aapl HTTP/1.1

> User-Agent: curl/7.33.0

> Host: api.tradeking.com

> Accept: */*

> 'Authorization:OAuth

> 

< HTTP/1.1 411 Length Required

< Connection: close

< Content-Length: 284

< 

{ [data not shown]

100   284  100   284    0     0    178      0  0:00:01  0:00:01 --:--:--   178
* Closing connection 0
* SSLv3, TLS alert, Client hello (1):
} [data not shown]
* Rebuilt URL to:
oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0,/
* Adding handle: conn: 0x1f00560
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 1 (0x1f00560) send_pipe: 1, recv_pipe: 0
* Could not resolve host:
oauth_nonce=,oauth_timestamp=1359019570,oauth_version=1.0,
* Closing connection 1
curl: (6) Could not resolve host:
oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0,

[1/3]:
oauth_signature_method=HMAC-SHA1,oauth_consumer_key=
--> <stdout>
* Rebuilt URL to:
oauth_signature_method=HMAC-SHA1,oauth_consumer_key=
/
* Adding handle: conn: 0x1ef10e0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 2 (0x1ef10e0) send_pipe: 1, recv_pipe: 0
* Could not resolve host:
oauth_signature_method=HMAC-SHA1,oauth_consumer_key=

* Closing connection 2
curl: (6) Could not resolve host:
oauth_signature_method=HMAC-SHA1,oauth_consumer_key=


[1/2]:
oauth_token=,oauth_token_secret=
--> <stdout>
* Rebuilt URL to:
oauth_token=,oauth_token_secret=
/
* Adding handle: conn: 0x1ef56c0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 3 (0x1ef56c0) send_pipe: 1, recv_pipe: 0
* Could not resolve host:
oauth_token=,oauth_token_secret=
* Closing connection 3
curl: (6) Could not resolve host:
oauth_token=,oauth_token_secret=

NOTE: 12 records were read from the infile CURL.
      The minimum record length was 0.
      The maximum record length was 174.
NOTE: DATA statement used (Total process time):
      real time           1.84 seconds
      cpu time            0.23 seconds
1
If you grab the same data via a web browser, are there any headers it sends that you're not sending already in your code that might be required in order for this to work? - user667489
Thanks for your thoughts. How do you test an api via web browser? I installed ActivePython-2.7.8.10-win64-x64 and Twisted-14.0.2.win-amd64-py2.7, but couldn't get anything to work. Is there an easier way? - user3806824
Sorry, that part didn't scan properly when I read your question. The general point still applies, though - if you've got a working example API call (i.e. not using cURL), look at the traffic generated and see if you've left anything out from your cURL call. - user667489

1 Answers

0
votes

The first thing to do is to try running your cURL statement from the command line. If you get a valid response it's a SAS issue, otherwise it's an issue with your cURL request.

Have you tried adding the -k option to your cURL statement seeing as you're making an https request?

Oh also, do any of the macro variables contain values that include ampersands, percent symbols, double quotes, or single quotes? These could all cause issues - especially ampersands as they need to be further masked with a caret symbol ^.