@@ -202,3 +202,104 @@ wrapper, you access the information directly using property syntax.
202
202
format-err("curl easy perform failed: %s\n",
203
203
err.curl-error-message)
204
204
end block;
205
+
206
+ Headers
207
+ -------
208
+
209
+ Libcurl offers manipulation of HTTP headers using `CURLOPT_HTTPHEADER `
210
+ option.
211
+
212
+ Libcurl's `CURLOPT_HTTPHEADER ` option allows you to send custom HTTP
213
+ headers with your requests. The option accepts a linked list of
214
+ header strings, which you build using `curl_slist ` and `curl_slist_append `.
215
+
216
+ .. code-block :: C
217
+ :caption: Libcurl example using HTTP headers
218
+
219
+ CURL *curl;
220
+ CURLcode res;
221
+
222
+ curl_global_init(CURL_GLOBAL_DEFAULT);
223
+ curl = curl_easy_init();
224
+
225
+ struct curl_slist *headers = NULL;
226
+ headers = curl_slist_append(headers, "Content-Type: application/json");
227
+ headers = curl_slist_append(headers, "Authorization: Bearer your_token_here");
228
+ headers = curl_slist_append(headers, "X-Custom-Header: Custom Value");
229
+
230
+ if (curl) {
231
+ curl_easy_setopt(curl, CURLOPT_URL, "https://api.example.com/data");
232
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
233
+
234
+ // ... other curl options ...
235
+
236
+ res = curl_easy_perform(curl);
237
+
238
+ if (res != CURLE_OK) {
239
+ fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
240
+ }
241
+
242
+ curl_slist_free_all(headers);
243
+ curl_easy_cleanup(curl);
244
+ }
245
+ curl_global_cleanup();
246
+
247
+ The above example could be translated to Opendylan in three ways:
248
+
249
+ .. code-block :: dylan
250
+ :caption: Using `add-header!`
251
+
252
+ with-curl-global($curl-global-default)
253
+ with-curl-easy(curl = make(<curl-easy>),
254
+ url = "https://api.example.com/data")
255
+
256
+ add-header!(curl,
257
+ "Content-Type: application/json",
258
+ "Authorization: Bearer your_token_here",
259
+ "X-Custom-Header: Custom Value");
260
+
261
+ // ... other curl options ...
262
+
263
+ curl-easy-perform(curl);
264
+
265
+ end with-curl-easy;
266
+ end with-curl-global;
267
+
268
+ Or passing the options to :macro: `with-curl-easy `:
269
+
270
+ .. code-block :: dylan
271
+ :caption: Using `with-curl-easy` options
272
+
273
+ with-curl-global($curl-global-default)
274
+ with-curl-easy(curl = make(<curl-easy>),
275
+ url = "https://api.example.com/data",
276
+ header = "Content-Type: application/json",
277
+ header = "Authorization: Bearer your_token_here",
278
+ header = "X-Custom-Header: Custom Value")
279
+
280
+ // ... other curl options ...
281
+
282
+ curl-easy-perform(curl);
283
+
284
+ end with-curl-easy;
285
+ end with-curl-global;
286
+
287
+ Or using the setter method :meth: `curl-header-setter `
288
+
289
+ .. code-block :: dylan
290
+ :caption: Using `curl-header-setter`
291
+
292
+ with-curl-global($curl-global-default)
293
+ with-curl-easy(curl = make(<curl-easy>),
294
+ url = "https://api.example.com/data")
295
+
296
+ curl.curl-header := "Content-Type: application/json";
297
+ curl.curl-header := "Authorization: Bearer your_token_here";
298
+ curl.curl-header := "X-Custom-Header: Custom Value";
299
+
300
+ // ... other curl options ...
301
+
302
+ curl-easy-perform(curl);
303
+
304
+ end with-curl-easy;
305
+ end with-curl-global;
0 commit comments