Communicating to an Application Programming Interface using cURL

by Applied Informatics

Nowadays, we use API’s for almost every project. APIs have a major role in mobile friendly portals as well, where a mobile application can interact with the server through API calls provided by the utility. cURL is a command-line utility (Linux or Mac OS X) for interacting with URLs. It is useful for requesting and posting data from APIs. These request will be processed with the headers and the data attached with the command. In this post we can discuss how to use the popular command line utility curl to interact with APIs.


HTTP requests to a Application Programming Interface resource will have the following data:

  • HTTP method (HEAD, GET, POST, UPDATE, or DELETE). The API will handle each of these methods simultaneously.
  • Access tokens can be used to authenticate a request which can be generated from the API itself.
  • Data can be sent to the API interface in JSON or XML. Though in most cases we might be using JSON.
  • Any POST request will be having data in JSON or XML for updating a record with new information.
The HTTP methods which are being used to retrieve information, as well as creating, updating, and deleting instances.

  • HEAD is used to retrieve metainformation.
  • GET is used to extract information from the API interface.
  • POST can be used to add a new instance.
  • UPDATE is used to update an existing instance.
  • DELETE is used to delete an existing instance.

To access any resource in the API interface, type an HTTP request containing a url, header, method and data if required.

Let’s discuss few POST calls to get a better understanding.

We can use the -X(request) flag along with -d(data)  to POST data

curl --user "test user" -X POST --data '{"remarks":"Created via API","private":"true","docs":{"test_file1.txt":{"content":"Demo"}}'

We can say that --data implies POST so you don’t have to also specify the --request flag

curl --user "test user" --data '{"remarks":"Created via API","private":"true","docs":{"test_file1.txt":{"content":"Demo"}}'

Here is an example that uses an API using auth token.

curl --data "login=test_user" --data "token=fjo23o4w9fw8er9342i387r"

We can join the post data into a single –data flag which seems to be a simpler way of using it.

curl --data "login=test_user&token=dftg45tetgrg54tyrtg45dgdfg54t"

We can use curl to read from STDIN(@-) to POST data.

curl --user "test_user” –data @-

{ "description":"Test",
  "docs": { "test_file1.txt": { "content":"Demo" } 

5 thoughts on “Communicating to an Application Programming Interface using cURL”

  1. Thanks, this is very helpful tutorial, concise and clear. there is small syntax error in your model please fix that.
    text = models.Charfield(max_length=255)
    notes = models.Charfield(max_length=255)

    And I also have question, Can I create two forms from one model, I want to do user registration(form 1) and company registration(form 2) each user should associated with company registration, DO I need to make two models or just one should be fine?

    1. Thanks for pointing out the syntax error.
      Regarding the question yes you can create two forms from one model.You can create two models one for user and another for company.Depending on your need you can relate the two models by foreign key or many-to-many or simply one-to-one relation.

  2. I have tried this but not working.

    JSONObject obj = new JSONObject();
    try {
    obj.put(“Answer ID “, get_answer_id);
    obj.put(“User Name “, get_name_of_user);
    obj.put(“Mobile “, get_mobile_of_user);
    obj.put(“Question”, get_question);
    obj.put(“Answer”, get_answer);

    } catch (JSONException e) {
    // TODO Auto-generated catch block


    // send the json data to the next class
    if (obj.length() > 0) {
    new SendJSONtoServer(this).execute(String.valueOf(obj));


    this is the code i used in my application. I have copied your code and paste that into SendJSONtoServer.class. but the code is not working and have some error in below

    //send to post execute
    return JsonResponse;
    } catch (JSONException e) { (ERROR IS HERE IN JSONException e)
    return null;

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts