admin管理员组文章数量:1431391
EDIT : the API developer provided a solution by using another delimiter and specifying it in the request (see below my answer to my own question)
I am sending POST requests to a RESTful API, which require a ma-separated list of arguments :
var request = require('request-promise'); //promisified npm request
// the list of names is huge
// those names are stored in a MongoDB database
// the namesList is generated programmatically before the request
var namesList = "name1,name2,name3,name4"
var requestOptions = {
method: 'POST',
uri: 'https://myAPI/someEndPoint/',
body: {
key: myAccessKey,
names: namesList
},
json: true
};
request(requestOptions)
.then( () => {_do_something_} );
EDIT : the API developer provided a solution by using another delimiter and specifying it in the request (see below my answer to my own question)
I am sending POST requests to a RESTful API, which require a ma-separated list of arguments :
var request = require('request-promise'); //promisified npm request
// the list of names is huge
// those names are stored in a MongoDB database
// the namesList is generated programmatically before the request
var namesList = "name1,name2,name3,name4"
var requestOptions = {
method: 'POST',
uri: 'https://myAPI/someEndPoint/',
body: {
key: myAccessKey,
names: namesList
},
json: true
};
request(requestOptions)
.then( () => {_do_something_} );
It works fine for most of the names, but some of them contain a ma :
var arrayNames = ["foo bar", "barfoo", "stupid, ma", "dammit"];
// is converted by my code in :
var namesList = "foo bar,barfoo,stupid, ma, dammit";
This inevitably leads to a wrong list being sent to the API... So, is there a way to "escape" the faulty ma programmatically when I generate the list from the array ?
Share Improve this question edited Jun 14, 2022 at 21:22 bad_coder 13k20 gold badges56 silver badges88 bronze badges asked Aug 8, 2016 at 2:59 klonawayklonaway 4811 gold badge4 silver badges20 bronze badges 4- 1 So check the documentation of that service about how to escape it. It is up to the service developer on how to serialise data. – zerkms Commented Aug 8, 2016 at 3:08
- @zerkms : You wouldn't believe how minimalist their documentation is, and how long it takes to get a proper response for my question... It's not a clean GitHub repo, far from that. Besides, I asked here because I thought that might be a code mistake on my side ;-) – klonaway Commented Aug 8, 2016 at 3:11
- 1 Well, it's ONLY the developer of the API that can tell you how they expect you to escape it. It is them who developed this odd way of serialising an array of strings, and we have no idea what they had in mind when they designed it and how they assume you to transfer strings with ma inside. – zerkms Commented Aug 8, 2016 at 3:11
- @zerkms : oh well... you're killing me, but yeah, makes sense... – klonaway Commented Aug 8, 2016 at 3:13
3 Answers
Reset to default 2The long awaited answer from the API developer has arrived (sent an e-mail a while ago), and the solution is as simple as it is efficient : just use another delimiter :
var namesList = "name1;name2;name3;name4" // use ';' instead of ',' here...
var requestOptions = {
method: 'POST',
uri: 'https://myAPI/someEndPoint/',
body: {
key: myAccessKey,
names: namesList,
delimiter: ';' // and specify the delimiter there !
},
json: true
};
request(requestOptions)
.then( () => {_do_something_} );
I don't know if the delimiter
field is standard or specific to this API, but it works perfectly for my use case !
You could iterate thru your list that you are sending, enclose ma inside a string with double quotes. Then handle those double quotes on the server side. This will preserve your list integrity
use something like this to replace subString:
str = str.replace(/,/g, '","');
On the serverSide, you can then ignore all mas wrapped inside double quotes.
try
var namesList = arrayNames.map(function(val) { return val.replace(',', '\\,');}).toString();
本文标签: javascripthow to quotescapequot a comma in a commaseparated list in a POST requestStack Overflow
版权声明:本文标题:javascript - how to "escape" a comma in a comma-separated list in a POST request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584580a2664821.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论