Bad request when performing a POST API query using webwrite











up vote
1
down vote

favorite












I'm attempting to create an App that exchanges information with an api (Questrade API to be precise). The api uses oauth2 for security and authentication.



I have been successful1 in acquiring authorization tokens, refresh tokens and querying basic information (my account information and stock quotes).



However, I'm encountering an issue when I try to query stock option quotes (as described here).



I've tried many different permutations but to no avail. This is what I've done so far:



1). I've taken the example shown in the "Sample Request" section provided in the link above and performed jsondecode to get the MATLAB equivalent and the "exact template" for which the parameter structure must be coded:



>> eg_param = jsondecode('{"filters":{"optionType":"Call","underlyingId":27426,"expiryDate":"2017-01-20T00:00:00.000000-05:00","minstrikePrice":70,"maxstrikePrice":80},"optionIds":[9907637, 9907638]}')

eg_param =
struct with fields:
filters: [1×1 struct]
optionIds: [2×1 double]

>> eg_param.filters
ans =
struct with fields:
optionType: 'Call'
underlyingId: 27426
expiryDate: '2017-01-20T00:00:00.000000-05:00'
minstrikePrice: 70
maxstrikePrice: 80

>> eg_param.optionIds
ans =
9907637
9907638


2). Substituted my actual values (real_params) for those in the example:



real_params.optionIds = 23255262;
real_params.filters.optionType = 'Call';
real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
real_params.filters.underlyingId = 40825;
real_params.filters.minstrikePrice = ;
real_params.filters.maxstrikePrice = ;


3). Changed weboptions' RequestMethod paramater to post



web_opt.RequestMethod = 'Post';


4). Used the webwrite function, as opposed to 'webread' to post and query the server:



new_data = webwrite(['https://api01.iq.questrade.com/',...
'v1/markets/quotes/options'], real_params, web_opt);


However, when I do this, is get the error message:



*The server returned the status 400 with
message "Bad Request" in response to the
request to URL
https://api01.iq.questrade.com/v1/markets/quotes/options.*


I've tried many different permutations including changing brackets, , {},{{}} and using different values for the parameters however the result is the same. Also, whenever I tested, I made sure to refresh the access token and test that the connection works using the "account info" request, so this error doesn't have to do with any authorization, security or connection issue.





1 For example, to acquire account information, which the website instructs as:



GET https://api01.iq.questrade.com/v1/accounts


I've 1). used MATLAB's weboptions and created an object to store token information in HeaderFields:



web_opt = weboptions;
web_opt.RequestMethod = 'Get';
headerFields = {'Authorization', ['Bearer ', 'ZHHgMgh0up5UqJ9TSOIALpkoVpi0']};
web_opt.HeaderFields = headerFields;


and 2). queried the server using:



data = webread(['https://api01.iq.questrade.com/', 'v1/accounts'], web_opt);


And this accomplishes communication with the API server and MATLAB stores my account information as a structure array in the variable data.










share|improve this question
























  • Unfortunately I thought I added hyperlinks to the questions but they are not showing up in the original message. The links are: [website][1] - questrade.com/api/documentation/rest-operations/account-calls/… (as described [here][2]. - questrade.com/api/documentation/rest-operations/market-calls/…
    – kquests
    Nov 7 at 11:22















up vote
1
down vote

favorite












I'm attempting to create an App that exchanges information with an api (Questrade API to be precise). The api uses oauth2 for security and authentication.



I have been successful1 in acquiring authorization tokens, refresh tokens and querying basic information (my account information and stock quotes).



However, I'm encountering an issue when I try to query stock option quotes (as described here).



I've tried many different permutations but to no avail. This is what I've done so far:



1). I've taken the example shown in the "Sample Request" section provided in the link above and performed jsondecode to get the MATLAB equivalent and the "exact template" for which the parameter structure must be coded:



>> eg_param = jsondecode('{"filters":{"optionType":"Call","underlyingId":27426,"expiryDate":"2017-01-20T00:00:00.000000-05:00","minstrikePrice":70,"maxstrikePrice":80},"optionIds":[9907637, 9907638]}')

eg_param =
struct with fields:
filters: [1×1 struct]
optionIds: [2×1 double]

>> eg_param.filters
ans =
struct with fields:
optionType: 'Call'
underlyingId: 27426
expiryDate: '2017-01-20T00:00:00.000000-05:00'
minstrikePrice: 70
maxstrikePrice: 80

>> eg_param.optionIds
ans =
9907637
9907638


2). Substituted my actual values (real_params) for those in the example:



real_params.optionIds = 23255262;
real_params.filters.optionType = 'Call';
real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
real_params.filters.underlyingId = 40825;
real_params.filters.minstrikePrice = ;
real_params.filters.maxstrikePrice = ;


3). Changed weboptions' RequestMethod paramater to post



web_opt.RequestMethod = 'Post';


4). Used the webwrite function, as opposed to 'webread' to post and query the server:



new_data = webwrite(['https://api01.iq.questrade.com/',...
'v1/markets/quotes/options'], real_params, web_opt);


However, when I do this, is get the error message:



*The server returned the status 400 with
message "Bad Request" in response to the
request to URL
https://api01.iq.questrade.com/v1/markets/quotes/options.*


I've tried many different permutations including changing brackets, , {},{{}} and using different values for the parameters however the result is the same. Also, whenever I tested, I made sure to refresh the access token and test that the connection works using the "account info" request, so this error doesn't have to do with any authorization, security or connection issue.





1 For example, to acquire account information, which the website instructs as:



GET https://api01.iq.questrade.com/v1/accounts


I've 1). used MATLAB's weboptions and created an object to store token information in HeaderFields:



web_opt = weboptions;
web_opt.RequestMethod = 'Get';
headerFields = {'Authorization', ['Bearer ', 'ZHHgMgh0up5UqJ9TSOIALpkoVpi0']};
web_opt.HeaderFields = headerFields;


and 2). queried the server using:



data = webread(['https://api01.iq.questrade.com/', 'v1/accounts'], web_opt);


And this accomplishes communication with the API server and MATLAB stores my account information as a structure array in the variable data.










share|improve this question
























  • Unfortunately I thought I added hyperlinks to the questions but they are not showing up in the original message. The links are: [website][1] - questrade.com/api/documentation/rest-operations/account-calls/… (as described [here][2]. - questrade.com/api/documentation/rest-operations/market-calls/…
    – kquests
    Nov 7 at 11:22













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm attempting to create an App that exchanges information with an api (Questrade API to be precise). The api uses oauth2 for security and authentication.



I have been successful1 in acquiring authorization tokens, refresh tokens and querying basic information (my account information and stock quotes).



However, I'm encountering an issue when I try to query stock option quotes (as described here).



I've tried many different permutations but to no avail. This is what I've done so far:



1). I've taken the example shown in the "Sample Request" section provided in the link above and performed jsondecode to get the MATLAB equivalent and the "exact template" for which the parameter structure must be coded:



>> eg_param = jsondecode('{"filters":{"optionType":"Call","underlyingId":27426,"expiryDate":"2017-01-20T00:00:00.000000-05:00","minstrikePrice":70,"maxstrikePrice":80},"optionIds":[9907637, 9907638]}')

eg_param =
struct with fields:
filters: [1×1 struct]
optionIds: [2×1 double]

>> eg_param.filters
ans =
struct with fields:
optionType: 'Call'
underlyingId: 27426
expiryDate: '2017-01-20T00:00:00.000000-05:00'
minstrikePrice: 70
maxstrikePrice: 80

>> eg_param.optionIds
ans =
9907637
9907638


2). Substituted my actual values (real_params) for those in the example:



real_params.optionIds = 23255262;
real_params.filters.optionType = 'Call';
real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
real_params.filters.underlyingId = 40825;
real_params.filters.minstrikePrice = ;
real_params.filters.maxstrikePrice = ;


3). Changed weboptions' RequestMethod paramater to post



web_opt.RequestMethod = 'Post';


4). Used the webwrite function, as opposed to 'webread' to post and query the server:



new_data = webwrite(['https://api01.iq.questrade.com/',...
'v1/markets/quotes/options'], real_params, web_opt);


However, when I do this, is get the error message:



*The server returned the status 400 with
message "Bad Request" in response to the
request to URL
https://api01.iq.questrade.com/v1/markets/quotes/options.*


I've tried many different permutations including changing brackets, , {},{{}} and using different values for the parameters however the result is the same. Also, whenever I tested, I made sure to refresh the access token and test that the connection works using the "account info" request, so this error doesn't have to do with any authorization, security or connection issue.





1 For example, to acquire account information, which the website instructs as:



GET https://api01.iq.questrade.com/v1/accounts


I've 1). used MATLAB's weboptions and created an object to store token information in HeaderFields:



web_opt = weboptions;
web_opt.RequestMethod = 'Get';
headerFields = {'Authorization', ['Bearer ', 'ZHHgMgh0up5UqJ9TSOIALpkoVpi0']};
web_opt.HeaderFields = headerFields;


and 2). queried the server using:



data = webread(['https://api01.iq.questrade.com/', 'v1/accounts'], web_opt);


And this accomplishes communication with the API server and MATLAB stores my account information as a structure array in the variable data.










share|improve this question















I'm attempting to create an App that exchanges information with an api (Questrade API to be precise). The api uses oauth2 for security and authentication.



I have been successful1 in acquiring authorization tokens, refresh tokens and querying basic information (my account information and stock quotes).



However, I'm encountering an issue when I try to query stock option quotes (as described here).



I've tried many different permutations but to no avail. This is what I've done so far:



1). I've taken the example shown in the "Sample Request" section provided in the link above and performed jsondecode to get the MATLAB equivalent and the "exact template" for which the parameter structure must be coded:



>> eg_param = jsondecode('{"filters":{"optionType":"Call","underlyingId":27426,"expiryDate":"2017-01-20T00:00:00.000000-05:00","minstrikePrice":70,"maxstrikePrice":80},"optionIds":[9907637, 9907638]}')

eg_param =
struct with fields:
filters: [1×1 struct]
optionIds: [2×1 double]

>> eg_param.filters
ans =
struct with fields:
optionType: 'Call'
underlyingId: 27426
expiryDate: '2017-01-20T00:00:00.000000-05:00'
minstrikePrice: 70
maxstrikePrice: 80

>> eg_param.optionIds
ans =
9907637
9907638


2). Substituted my actual values (real_params) for those in the example:



real_params.optionIds = 23255262;
real_params.filters.optionType = 'Call';
real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
real_params.filters.underlyingId = 40825;
real_params.filters.minstrikePrice = ;
real_params.filters.maxstrikePrice = ;


3). Changed weboptions' RequestMethod paramater to post



web_opt.RequestMethod = 'Post';


4). Used the webwrite function, as opposed to 'webread' to post and query the server:



new_data = webwrite(['https://api01.iq.questrade.com/',...
'v1/markets/quotes/options'], real_params, web_opt);


However, when I do this, is get the error message:



*The server returned the status 400 with
message "Bad Request" in response to the
request to URL
https://api01.iq.questrade.com/v1/markets/quotes/options.*


I've tried many different permutations including changing brackets, , {},{{}} and using different values for the parameters however the result is the same. Also, whenever I tested, I made sure to refresh the access token and test that the connection works using the "account info" request, so this error doesn't have to do with any authorization, security or connection issue.





1 For example, to acquire account information, which the website instructs as:



GET https://api01.iq.questrade.com/v1/accounts


I've 1). used MATLAB's weboptions and created an object to store token information in HeaderFields:



web_opt = weboptions;
web_opt.RequestMethod = 'Get';
headerFields = {'Authorization', ['Bearer ', 'ZHHgMgh0up5UqJ9TSOIALpkoVpi0']};
web_opt.HeaderFields = headerFields;


and 2). queried the server using:



data = webread(['https://api01.iq.questrade.com/', 'v1/accounts'], web_opt);


And this accomplishes communication with the API server and MATLAB stores my account information as a structure array in the variable data.







json matlab api post oauth-2.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 13:43









Dev-iL

16.3k64074




16.3k64074










asked Nov 7 at 11:15









kquests

61




61












  • Unfortunately I thought I added hyperlinks to the questions but they are not showing up in the original message. The links are: [website][1] - questrade.com/api/documentation/rest-operations/account-calls/… (as described [here][2]. - questrade.com/api/documentation/rest-operations/market-calls/…
    – kquests
    Nov 7 at 11:22


















  • Unfortunately I thought I added hyperlinks to the questions but they are not showing up in the original message. The links are: [website][1] - questrade.com/api/documentation/rest-operations/account-calls/… (as described [here][2]. - questrade.com/api/documentation/rest-operations/market-calls/…
    – kquests
    Nov 7 at 11:22
















Unfortunately I thought I added hyperlinks to the questions but they are not showing up in the original message. The links are: [website][1] - questrade.com/api/documentation/rest-operations/account-calls/… (as described [here][2]. - questrade.com/api/documentation/rest-operations/market-calls/…
– kquests
Nov 7 at 11:22




Unfortunately I thought I added hyperlinks to the questions but they are not showing up in the original message. The links are: [website][1] - questrade.com/api/documentation/rest-operations/account-calls/… (as described [here][2]. - questrade.com/api/documentation/rest-operations/market-calls/…
– kquests
Nov 7 at 11:22












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Unfortunately I cannot test the solution since the API key you provided seems not to work, but it might help you anyway.



Can you spot the difference between the JSON produced using these two blocks?



% Original code:
real_params.optionIds = 23255262;
real_params.filters.optionType = 'Call';
real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
real_params.filters.underlyingId = 40825;
real_params.filters.minstrikePrice = ;
real_params.filters.maxstrikePrice = ;
%{
>> jsonencode(real_params)
ans =
'{"optionIds":2.3255262E+7,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
%}





% Slightly modified code:
real_params = struct();
real_params.optionIds = int32(23255262);
real_params.filters.optionType = 'Call';
real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
real_params.filters.underlyingId = 40825;
real_params.filters.minstrikePrice = ;
real_params.filters.maxstrikePrice = ;
%{
>> jsonencode(real_params)
ans =
'{"optionIds":23255262,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
%}


It might be that the automatic conversion is the culprit in this case (which for a lack of better reason, sent your optionIds as a double value, and not the integer that the API expects.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188391%2fbad-request-when-performing-a-post-api-query-using-webwrite%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Unfortunately I cannot test the solution since the API key you provided seems not to work, but it might help you anyway.



    Can you spot the difference between the JSON produced using these two blocks?



    % Original code:
    real_params.optionIds = 23255262;
    real_params.filters.optionType = 'Call';
    real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
    real_params.filters.underlyingId = 40825;
    real_params.filters.minstrikePrice = ;
    real_params.filters.maxstrikePrice = ;
    %{
    >> jsonencode(real_params)
    ans =
    '{"optionIds":2.3255262E+7,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
    %}





    % Slightly modified code:
    real_params = struct();
    real_params.optionIds = int32(23255262);
    real_params.filters.optionType = 'Call';
    real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
    real_params.filters.underlyingId = 40825;
    real_params.filters.minstrikePrice = ;
    real_params.filters.maxstrikePrice = ;
    %{
    >> jsonencode(real_params)
    ans =
    '{"optionIds":23255262,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
    %}


    It might be that the automatic conversion is the culprit in this case (which for a lack of better reason, sent your optionIds as a double value, and not the integer that the API expects.






    share|improve this answer

























      up vote
      1
      down vote













      Unfortunately I cannot test the solution since the API key you provided seems not to work, but it might help you anyway.



      Can you spot the difference between the JSON produced using these two blocks?



      % Original code:
      real_params.optionIds = 23255262;
      real_params.filters.optionType = 'Call';
      real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
      real_params.filters.underlyingId = 40825;
      real_params.filters.minstrikePrice = ;
      real_params.filters.maxstrikePrice = ;
      %{
      >> jsonencode(real_params)
      ans =
      '{"optionIds":2.3255262E+7,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
      %}





      % Slightly modified code:
      real_params = struct();
      real_params.optionIds = int32(23255262);
      real_params.filters.optionType = 'Call';
      real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
      real_params.filters.underlyingId = 40825;
      real_params.filters.minstrikePrice = ;
      real_params.filters.maxstrikePrice = ;
      %{
      >> jsonencode(real_params)
      ans =
      '{"optionIds":23255262,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
      %}


      It might be that the automatic conversion is the culprit in this case (which for a lack of better reason, sent your optionIds as a double value, and not the integer that the API expects.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Unfortunately I cannot test the solution since the API key you provided seems not to work, but it might help you anyway.



        Can you spot the difference between the JSON produced using these two blocks?



        % Original code:
        real_params.optionIds = 23255262;
        real_params.filters.optionType = 'Call';
        real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
        real_params.filters.underlyingId = 40825;
        real_params.filters.minstrikePrice = ;
        real_params.filters.maxstrikePrice = ;
        %{
        >> jsonencode(real_params)
        ans =
        '{"optionIds":2.3255262E+7,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
        %}





        % Slightly modified code:
        real_params = struct();
        real_params.optionIds = int32(23255262);
        real_params.filters.optionType = 'Call';
        real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
        real_params.filters.underlyingId = 40825;
        real_params.filters.minstrikePrice = ;
        real_params.filters.maxstrikePrice = ;
        %{
        >> jsonencode(real_params)
        ans =
        '{"optionIds":23255262,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
        %}


        It might be that the automatic conversion is the culprit in this case (which for a lack of better reason, sent your optionIds as a double value, and not the integer that the API expects.






        share|improve this answer












        Unfortunately I cannot test the solution since the API key you provided seems not to work, but it might help you anyway.



        Can you spot the difference between the JSON produced using these two blocks?



        % Original code:
        real_params.optionIds = 23255262;
        real_params.filters.optionType = 'Call';
        real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
        real_params.filters.underlyingId = 40825;
        real_params.filters.minstrikePrice = ;
        real_params.filters.maxstrikePrice = ;
        %{
        >> jsonencode(real_params)
        ans =
        '{"optionIds":2.3255262E+7,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
        %}





        % Slightly modified code:
        real_params = struct();
        real_params.optionIds = int32(23255262);
        real_params.filters.optionType = 'Call';
        real_params.filters.expiryDate ='2018-11-09T00:00:00.000000-05:00';
        real_params.filters.underlyingId = 40825;
        real_params.filters.minstrikePrice = ;
        real_params.filters.maxstrikePrice = ;
        %{
        >> jsonencode(real_params)
        ans =
        '{"optionIds":23255262,"filters":{"optionType":"Call","expiryDate":"2018-11-09T00:00:00.000000-05:00","underlyingId":40825,"minstrikePrice":,"maxstrikePrice":}}'
        %}


        It might be that the automatic conversion is the culprit in this case (which for a lack of better reason, sent your optionIds as a double value, and not the integer that the API expects.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 14:04









        Dev-iL

        16.3k64074




        16.3k64074






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188391%2fbad-request-when-performing-a-post-api-query-using-webwrite%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini