mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-05-31 00:00:07 +08:00
fix #71: encode query string in URL correctly on Android
This commit is contained in:
@@ -363,26 +363,35 @@ public class HttpRequest {
|
||||
}
|
||||
|
||||
private static StringBuilder addParam(final Object key, Object value,
|
||||
final StringBuilder result) {
|
||||
final StringBuilder result) throws HttpRequestException {
|
||||
return addParam(key, value, result, CHARSET_UTF8);
|
||||
}
|
||||
|
||||
private static StringBuilder addParam(final Object key, Object value,
|
||||
final StringBuilder result, String charset) throws HttpRequestException {
|
||||
if (value != null && value.getClass().isArray())
|
||||
value = arrayToList(value);
|
||||
|
||||
if (value instanceof Iterable<?>) {
|
||||
Iterator<?> iterator = ((Iterable<?>) value).iterator();
|
||||
while (iterator.hasNext()) {
|
||||
result.append(key);
|
||||
result.append("[]=");
|
||||
Object element = iterator.next();
|
||||
if (element != null)
|
||||
result.append(element);
|
||||
if (iterator.hasNext())
|
||||
result.append("&");
|
||||
try {
|
||||
if (value instanceof Iterable<?>) {
|
||||
Iterator<?> iterator = ((Iterable<?>) value).iterator();
|
||||
while (iterator.hasNext()) {
|
||||
result.append(URLEncoder.encode(key.toString(), charset));
|
||||
result.append("[]=");
|
||||
Object element = iterator.next();
|
||||
if (element != null)
|
||||
result.append(URLEncoder.encode(element.toString(), charset));
|
||||
if (iterator.hasNext())
|
||||
result.append("&");
|
||||
}
|
||||
} else {
|
||||
result.append(URLEncoder.encode(key.toString(), charset));
|
||||
result.append("=");
|
||||
if (value != null)
|
||||
result.append(URLEncoder.encode(value.toString(), charset));
|
||||
}
|
||||
} else {
|
||||
result.append(key);
|
||||
result.append("=");
|
||||
if (value != null)
|
||||
result.append(value);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new HttpRequestException(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -384,7 +384,7 @@ const tests = [
|
||||
cookies.mySecondCookie.should.be.equal('mySecondValue');
|
||||
}
|
||||
},{
|
||||
description: 'should send UTF-8 encoded raw string correctly (POST)',
|
||||
description: 'should send UTF-8 encoded raw string correctly (POST) #34',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"data\\": \\"this is a test string\\"...',
|
||||
before: helpers.setUtf8StringSerializer,
|
||||
func: function(resolve, reject) {
|
||||
@@ -394,6 +394,17 @@ const tests = [
|
||||
result.type.should.be.equal('resolved');
|
||||
JSON.parse(result.data.data).data.should.eql('this is a test string');
|
||||
}
|
||||
},{
|
||||
description: 'should encode spaces in query string (params object) correctly (GET) #71',
|
||||
expected: 'resolved: {"status": 200, "data": "{\\"args\\": \\"query param\\": \\"and value with spaces\\"...',
|
||||
func: function(resolve, reject) {
|
||||
cordova.plugin.http.get('http://httpbin.org/get', { 'query param': 'and value with spaces' }, {}, resolve, reject);
|
||||
},
|
||||
validationFunc: function(driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
console.log(JSON.parse(result.data.data).args);
|
||||
JSON.parse(result.data.data).args['query param'].should.eql('and value with spaces');
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user