Commit cd8f62f
committed
requests: Support unicode in json and data payloads.
`json.dumps()` returns a str object, and if the JSON data contains unicode
then computing the length of this str -- which is used for "Content-Length"
-- will return the number of characters which will be shorter than the JSON
payload that is sent as bytes. The server will then read a truncated body.
Fix that by converting any str payload into bytes. This fix will also
convert any str object passed in by the user as the `data` argument, and
that actually seems to match the upstream requests behaviour (even if it's
documented that `data` should be bytes).
Note that `bytes(str, "utf-8")` does not make a copy of the str data, it
just makes a small bytes object that wraps the same underlying data. So
it's relatively efficient.
Fixes issue #251.
Signed-off-by: Damien George <damien@micropython.org>1 parent 4dfa813 commit cd8f62f
3 files changed
Lines changed: 59 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
126 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
127 | 130 | | |
128 | 131 | | |
129 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
87 | 136 | | |
88 | 137 | | |
89 | 138 | | |
| |||
157 | 206 | | |
158 | 207 | | |
159 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
160 | 213 | | |
161 | 214 | | |
162 | 215 | | |
| |||
0 commit comments