curl / Mailing Lists / curl-library / Single Mail

curl-library

Re: a URL API ?

From: Daniel Stenberg via curl-library <curl-library_at_cool.haxx.se>
Date: Thu, 9 Aug 2018 10:48:32 +0200 (CEST)

On Thu, 2 Aug 2018, Geoff Beier wrote:

> The setters would be important to us. I might be bikeshedding here, but the
> ability to add to the query would be very nice. So something like
> curl_url_query_append(urlp, "numitems", 3)

Returning to this, as I've polished the API a bit over the last few days. The
wiki page has been updated to reflect the changes I've done.

As the curl URL API works now, this is how you append a string to the query
of a URL.

First, create a handle and pass it a full URL:

  CURLU *h = curl_url();
  curl_url_set(h, CURLUPART_URL, "https://example.com/foo?askforthis", 0);

Say we want to append this to the query:

  char *append = "&thistoo=44";

We extract the query part

  char *q;
  curl_url_get(h, CURLUPART_QUERY, &q, 0);

Make space for the new enlarged query doing regular memory management and
create the updated querty there. The 'q' pointer points to memory managed by
libcurl so it can't be realloc'ed.

  char *newptr = malloc(strlen(q) + strlen(append) + 1);
  strcpy(newptr, q);
  strcat(newptr, append);

Then replace the former query part in the URL by setting this new one:

  curl_url_set(h, CURLUPART_QUERY, newptr, 0);

Free the data

  curl_free(q);
  free(newptr);

... and now we can extract the full URL again and it will have the updated
query part:

  char *url;
  curl_url_get(h, CURLUPART_URL, &url, 0);

-- 
  / daniel.haxx.se
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2018-08-09