5

I am new to C++ development and I have been trying to work with libcurl.

I basically am looking for a function that lets me enter in parameters similar to the -d flag in curl.

for example if I were to just send a url request in curl directly or the web browser the url would look like 'https://www.example.com/hello.json?whoisawesome=me&whosucks=yomomma'

then how could i feed it to libcurl not as just one url as suggested in the accepted answer of this question: Add paramethers to libcurl GET in c++

but as parameters I could add? I am hoping the call would automatically 'clean up' the text (e.g., to support ampersands(&) and equal signs (=) in the string. I'm sure there's other cases but that's the only one i can think of for now). Of course I could make a function that cleans up the string myself but I'd like to know if there's one built into libcurl.

I checked the api doc but I guuss I was not familiar enough to successfully navigate it to find what I needed.

Thanks for your time

3 Answers 3

7
  1. As already said, the full URL including GET parameters has to be built manually.
  2. Sanity checks, like stripping leading or trailing spaces, is not a responsibility of libcurl.
  3. To URL-encode the (sanitized) value part of a parameter you can use curl_easy_escape. The key part should be in your hand anyway.
Sign up to request clarification or add additional context in comments.

1 Comment

Ivan's answer is great but the library suggestion would definitely help others who are looking to resolve this problem, like I had.
1

You can't. You need to specify the full url that means it contains not only server name, but also schema, path, port and query (that is your GET parameters). I think it's the only non-freaked way of doing this.

Comments

1

I just use something like this in C++11:

typedef std::map<std::string, std::string> Params;

static std::string params_string(Params const &params)
{
  if(params.empty()) return "";
  Params::const_iterator pb= params.cbegin(), pe= params.cend();
  std::string data= pb-> first+ "="+ pb-> second;
  ++ pb; if(pb== pe) return data;
  for(; pb!= pe; ++ pb)
    data+= "&"+ pb-> first+ "="+ pb-> second;
  return data;
}

// ...

auto str= get("where.com?"+ params_string({ {"q", "C++"}, { "img", "true" } }));

// lets you write pretty API clients

3 Comments

maps are great managing the parameters but how did you solve the issue of 'cleaning up' the strings? Do we just have to do it ourselves or is there something prebuilt into libcurl?
@ThinkBonobo if you mean checking parameters for sanity, there are so many things to do. You would have to check if they represent the correct type of data, strip spaces, maybe even protect against injections. Since most parameters need custom validation, it is best done by hand.
it's for the very same reason that you mentioned, the need to take care of so many things that I was surprised that there was no functions that would clean the more commonly needed issues, particularly injection attacks, character replacements, and space stripping. Of course, there is always going to be a need for custom validation but having functions that take care of the 'boilerplate' would still have been nice. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.