Thanks a lot for your help! But sorry... I'm still confused about the function...
Here are some of my questions:
1.How to set the "fields" parameter? It seems like to be a string, but my api need more than one parameter. Could you please tell me how to deal with that?
For example, I can use my api in Python like this:
import requests
url="http://astronomy.ezgolife.com/api/o1.business/getFiles"
param={"observatory_id":21,"name":"123"}
response=requests.post(url,data=param)
json=response.json()
console.writeln(json)
and then I get response like this:
{
"code": 1,
"data": {
"files": []
},
"msg": ""
}
But when I use your function:
function POST( url, fields )
{
let T = new NetworkTransfer;
T.setURL( url );
T.response = new ByteArray;
T.onDownloadDataAvailable = function( data )
{
this.response.add( data );
};
if ( T.post( fields ) )
console.writeln( "<end><cbr>POST executed. Server response = '", T.response.toString(), "'" );
else
console.criticalln( "<end><cbr>*** Error: POST operation failed." );
}
POST("http://astronomy.ezgolife.com/api/o1.business/getFiles", "observatory_id=21&name=123");
It just raised the error. Could you please tell me what is the problem?
2. I guess that my error is because encoding, but I don't know how to use your second part of code to encode it...
I use it like this:
function toPercentEncoding( s )
{
function nibbleToHex( n )
{
const hexDigits = "0123456789ABCDEF";
return hexDigits[n & 0xf];
}
let e = new String;
for ( let i = 0, n = s.length; i < n; ++i )
{
let c = s.charCodeAt( i );
if ( c >= 0x61 && c <= 0x7A // a ... z
|| c >= 0x41 && c <= 0x5A // A ... Z
|| c >= 0x30 && c <= 0x39 // 0 ... 9
|| c == 0x2D // -
|| c == 0x2E // .
|| c == 0x5F // _
|| c == 0x7E ) // ~
{
e += s;
}
else
{
e += '%';
e += nibbleToHex( (c & 0xf0) >> 4 );
e += nibbleToHex( c & 0x0f );
}
}
return e;
}
console.writeln(toPercentEncoding("name"));
It returned:
name=123name=123name=123name=123%3Dname=123name=123name=123
Is that right? Should I use this result as parameter "fields"? In my opinion, it should be "e+=c" instead of "e+=s". Also, it seems like you forgot to encode '/'. But I'm not sure about that. Please tell me about that, thanks!!!