I am trying to create a Mathematica equivalent to the following code:

    curl 'https://signin.tradestation.com/oauth/token' \
--request POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--data 'grant_type=authorization_code' \
--data 'client_id=XXXXXXXXX' \
--data 'client_secret=XXXXXXXXX' \
--data 'redirect_uri=http://localhost' \
--data 'code=XXXXXXXXX'

curl 'https://signin.tradestation.com/oauth/token' \
--request POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-raw 'grant_type=authorization_code&client_id=XXXXXXXXX&client_secret=XXXXXXXXX&redirect_uri=http://localhost&code=XXXXXXXXX'

My Wolfram Language code equivalent looks like this:

urlToken = "https://signin.tradestation.com/oauth/token";
headers = {"Content-Type" -> "application/x-www-form-urlencoded"};
parameters = {"grant_type" -> "authorization_code", 
   "client_id" -> apiKey, "client_secret" -> clientSecret, 
   "code" -> AuthCode, "redirect_uri" -> urlRedirect };
request = 
   urlToken, <|"Method" -> "POST", "Headers" -> headers, 
    "Body" -> URLQueryEncode[parameters], "VerifyPeer" -> False|>];
response = URLRead[request]

Unfortunately all this produces is:

HTTPResponse[403 Status:Forbidden]

Can anyone advise what I am doing wrong?

Is there an easier approach?

  • $\begingroup$ It is a bit difficult to give any helpful feedback, because obviously we cannot test your code ... Have you tried with a bit different syntax: request = HTTPRequest[urlToken, <|"Body" -> parameters|>]? Does using curl works as expected? Also, what is the body of the response: response["Body"]? $\endgroup$
    – Domen
    Mar 22 at 19:58
  • $\begingroup$ Not sure if this will help, but I just solved a related issue comparing the good and the bad requests using mitmproxy. Also notice that the Method option is documented to be used without the quotes (it is a symbol, not a string). $\endgroup$ Mar 22 at 21:41


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.