Android Question How to Process a Comma in the PostString URL

swabygw

Active Member
Licensed User
Longtime User
This may be a common question, so sorry if this has been asked before. In my app, I send data to a database using the PostString command, like this:
B4X:
Dim MyHttpJob As HttpJob
MyHttpJob.PostString("http://www.mywebsite.com/webinterface.aspx?ID=0&Params=Param1,Param2", "Jobname")
This all works fine with the comma splitting up my parameters in Params. However, I need to send some data in the Params parameter that contains a comma, like this:
B4X:
Dim MyHttpJob As HttpJob
MyHttpJob.PostString("http://www.mywebsite.com/webinterface.aspx?ID=0&Params=Param3, is a good parameter.", "Jobname"))
In the example above, there is just one parameter in Params: "Param3, is a good parameter", but my .aspx code will see the comma in it and see it as two parameters. I could do something more unique, like creating another URL parameter, like &Params1 and &Params2, and not use a comma, but then the problem becomes what happens when there is an ampersand in the data.

The best solution I've found so far is to, for example, replace the comma with %2c in the URL and then, on the webserver side, replace %2c back to a comma. I don't like this solution, but it seems viable. Before I committed to that, though, I was wondering if there is anything in B4X that might do a URL rewriting job, or if anyone has a suggestion for a better way to accomplish this.
 

DonManfred

Expert
Licensed User
Longtime User
Why are you using poststring if you dont post anything?

Use
B4X:
job.Download2("http://www.mywebsite.com/webinterface.aspx",    Array As String("ID", "0", "Params", "Param3, is a good parameter."))
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
Good question - my thinking was that Download2 is a GET and I'm not "getting" anything, whereas PostString is a POST to update remote data. But I'm always confused by the differences between the POST, GET, and PUT. I'll try your suggestion.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
One question, though: with PostString, I can always test the URL in a browser to see if it's working - how can I test the Download2 idea in a browser (ie, outside of B4X)?
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
FYI - I tried your suggestion and your suggestion works beautifully. The testing aspect (using a browser) is an issue, but the benefits of the method you suggested outweigh that drawback...so, looks like I'll be doing a rewrite.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
One question, though: with PostString, I can always test the URL in a browser to see if it's working - how can I test the Download2 idea in a browser (ie, outside of B4X)?
Actually, the only reason that you are able to test the URL as you are using it with PostString (your initial post in this thread) is because you are not really using PostString to post your information (the only thing you are posting is "Jobname"). PostFile, PostBytes and PostString methods actually put nothing in the URL, but put the Parameters in the body of the request that is sent to the server. It just happens that you are still creating a manual URL submission, which is actually the same as using a GET (even though you are using a POST). Download2 (besides using a GET instead of POST) builds the part after the ? for you and URL-encodes everything not allowed for you. That is why the commas work that you are submitting (Download2 URL encoded them for you). So you still can test your server as is, you just have to manually URL-encode your submission so that you can submit such things as commas in your parameter. You can use https://www.urlencoder.org/ to help you encode your test data. Using that site, your
Param3, is a good parameter.
becomes
Param3%2C%20is%20a%20good%20parameter.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
You're right - it was only submitting/posting "Jobname". I've customized my .aspx page to handle both the GET and POST so that I can use Download2 for GET and also test the server code using a PostString URL to POST the test. That explanation was perfect - thanks!
 
Upvote 0
Top