Requete POST and base64 » History » Version 2
iri, 01/01/2012 10:59 PM
1 | 1 | iri | h1. Requete POST and base64 |
---|---|---|---|
2 | |||
3 | How to send datas using base64 encoding by a POST request ? |
||
4 | |||
5 | * Base64-encoded data takes about 33% more space than the original data. |
||
6 | * This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies. |
||
7 | |||
8 | You should know how to send a POST request : [[Request_POST]] before continue |
||
9 | |||
10 | In Scol, to encode to base64, use "_base64_encode_":http://www.scolring.org/files/doc_html/not_doc.html#kernel function. |
||
11 | |||
12 | <pre> |
||
13 | base64_encode : fun [S] S |
||
14 | </pre> |
||
15 | |||
16 | The first step is the encoding. The second step should be the conversion from any character to _web_ character : "_strtoweb_":http://www.scolring.org/files/doc_html/strtoweb.html |
||
17 | |||
18 | <pre> |
||
19 | strtoweb : fun [S] S |
||
20 | </pre> |
||
21 | |||
22 | This example sends an image to a server (via a PHP script). |
||
23 | |||
24 | <pre> |
||
25 | fun CBreceive (inet, u, data, err)= |
||
26 | if err == 0 then |
||
27 | ( |
||
28 | _fooS strcat ">>>>>>>CBreceive current>>>>>>> " data; |
||
29 | let u -> [s] in |
||
30 | mutate u <- [strcat s data]; |
||
31 | 0 |
||
32 | ) |
||
33 | else if err == 1 then |
||
34 | ( |
||
35 | let u -> [s] in |
||
36 | _fooS strcat ">>>>>>>CBreceive end>>>>>>> " s; |
||
37 | _closemachine |
||
38 | ) |
||
39 | else |
||
40 | ( |
||
41 | _fooS strcat ">>>>>>>CBreceive err>>>>>>> " itoa err; |
||
42 | 0 |
||
43 | );; |
||
44 | |||
45 | fun main ()= |
||
46 | _showconsole; |
||
47 | let _getpack _checkpack "examples/image.jpg" -> file in // get the file content |
||
48 | let base64_encode file -> file64 in // encode to base64 |
||
49 | let strtoweb file64 -> file64web in // convert to web character |
||
50 | let "http://domin.tld/post.php" -> url in |
||
51 | let strcat "image=" file64web -> data in |
||
52 | INETGetURLex2 _channel "POST" url "content-type: application/x-www-form-urlencoded" data 0 @CBreceive [""]; |
||
53 | 0;; |
||
54 | </pre> |
||
55 | |||
56 | 2 | iri | * With the SYSPACK API, you can use _base64encodePweb_ : |
57 | |||
58 | <pre> |
||
59 | fun CBreceive (inet, u, data, err)= |
||
60 | // the same thing than above |
||
61 | 0;; |
||
62 | |||
63 | fun main ()= |
||
64 | _showconsole; |
||
65 | let _getpack _checkpack "examples/image.jpg" -> file in // get the file content |
||
66 | let base64encodePweb file -> file64web in // encode to base64 and convert it |
||
67 | let "http://domin.tld/post.php" -> url in |
||
68 | let strcat "image=" file64web -> data in |
||
69 | INETGetURLex2 _channel "POST" url "content-type: application/x-www-form-urlencoded" data 0 @CBreceive [""]; |
||
70 | 0;; |
||
71 | </pre> |
||
72 | |||
73 | _base64encodePweb_ seems slightly faster than _base64_encode_ and _strtoweb_. _base64encodePweb_ is based from the PHP source code. |
||
74 | 1 | iri | |
75 | |||
76 | Author : iri |
||
77 | Date : december 2011 |
||
78 | |||
79 | *Return to [[Examples]]* |