Project

General

Profile

Requete POST and base64 » History » Version 1

iri, 12/31/2011 12:04 AM

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
57
58
Author : iri
59
Date   : december 2011
60
61
*Return to [[Examples]]*