1- function [str ,header ] = http_paramsToString(params ,encodeOption )
2- % http_paramsToString Creates string for a POST or GET requests
3- %
4- % [queryString,header] = http_paramsToString(params, *encodeOption)
5- %
6- % INPUTS
7- % =======================================================================
8- % params: cell array of property/value pairs
9- % NOTE: If the input is in a 2 column matrix, then first column
10- % entries are properties and the second column entries are
11- % values, however this is NOT necessary (generally linear)
12- % encodeOption: (default 1)
13- % 1 - the typical URL encoding scheme (Java call)
14- %
15- % OUTPUTS
16- % =======================================================================
17- % queryString: querystring to add onto URL (LACKS "?", see example)
18- % header : the header that should be attached for post requests when
19- % using urlread2
20- %
21- % EXAMPLE:
22- % ==============================================================
23- % params = {'cmd' 'search' 'db' 'pubmed' 'term' 'wtf batman'};
24- % queryString = http_paramsToString(params);
25- % queryString => cmd=search&db=pubmed&term=wtf+batman
26- %
27- % IMPORTANT: This function does not filter parameters, sort them,
28- % or remove empty inputs (if necessary), this must be done before hand
29-
30- if ~exist(' encodeOption' ,' var' )
31- encodeOption = 1 ;
32- end
33-
34- if size(params ,2 ) == 2 && size(params ,1 ) > 1
35- params = params ' ;
36- params = params(: );
37- end
38-
39- str = ' ' ;
40- for i= 1 : 2 : length(params )
41- if (i == 1 ), separator = ' ' ; else separator = ' &' ; end
42- switch encodeOption
43- case 1
44- param = urlencode(params{i });
45- value = urlencode(params{i + 1 });
46- % case 2
47- % param = oauth.percentEncodeString(params{i});
48- % value = oauth.percentEncodeString(params{i+1});
49- % header = http_getContentTypeHeader(1);
50- otherwise
51- error(' Case not used' )
52- end
53- str = [str separator param ' =' value ]; % #ok<AGROW>
54- end
55-
56- switch encodeOption
57- case 1
58- header = http_createHeader(' Content-Type' ,' application/x-www-form-urlencoded' );
59- end
60-
61-
1+ function [str ,header ] = http_paramsToString(params ,encodeOption )
2+ % http_paramsToString Creates string for a POST or GET requests
3+ %
4+ % [queryString,header] = http_paramsToString(params, *encodeOption)
5+ %
6+ % INPUTS
7+ % =======================================================================
8+ % params: cell array of property/value pairs
9+ % NOTE: If the input is in a 2 column matrix, then first column
10+ % entries are properties and the second column entries are
11+ % values, however this is NOT necessary (generally linear)
12+ % encodeOption: (default 1)
13+ % 1 - the typical URL encoding scheme (Java call)
14+ %
15+ % OUTPUTS
16+ % =======================================================================
17+ % queryString: querystring to add onto URL (LACKS "?", see example)
18+ % header : the header that should be attached for post requests when
19+ % using urlread2
20+ %
21+ % EXAMPLE:
22+ % ==============================================================
23+ % params = {'cmd' 'search' 'db' 'pubmed' 'term' 'wtf batman'};
24+ % queryString = http_paramsToString(params);
25+ % queryString => cmd=search&db=pubmed&term=wtf+batman
26+ %
27+ % IMPORTANT: This function does not filter parameters, sort them,
28+ % or remove empty inputs (if necessary), this must be done before hand
29+
30+ if ~exist(' encodeOption' ,' var' )
31+ encodeOption = 1 ;
32+ end
33+
34+ if size(params ,2 ) == 2 && size(params ,1 ) > 1
35+ params = params ' ;
36+ params = params(: );
37+ end
38+
39+ str = ' ' ;
40+ for i= 1 : 2 : length(params )
41+ if (i == 1 ), separator = ' ' ; else separator = ' &' ; end
42+ switch encodeOption
43+ case 1
44+ param = urlencode(params{i });
45+ value = urlencode(params{i + 1 });
46+ % case 2
47+ % param = oauth.percentEncodeString(params{i});
48+ % value = oauth.percentEncodeString(params{i+1});
49+ % header = http_getContentTypeHeader(1);
50+ otherwise
51+ error(' Case not used' )
52+ end
53+ str = [str separator param ' =' value ]; % #ok<AGROW>
54+ end
55+
56+ switch encodeOption
57+ case 1
58+ header = http_createHeader(' Content-Type' ,' application/x-www-form-urlencoded' );
59+ end
60+
61+
6262end
0 commit comments