JYOTI RANJAN PANDA
JYOTI RANJAN PANDA

Reputation: 122

RESTful client in c

Hi I want to make RESTful call to remote sever. url is http://bulksms.net/API.svc/sms/json/sendmessage?username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&dnRequired=false The server document says:

Parameters

string username, string password, string msg, string msisdn, string tagname, string shortcode, int telcoId, bool dnRequired

Return Value API will return and object containing three members IsSuccess Message UniqueId

I have made a sample client in c but its not working. below is my code.

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURL *curl;
  char url[80];
  CURLcode res;

  strcpy(url,"http://bulksms.net/API.svc/sms/json/sendmessage?username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&");

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_POST, url);

    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  return 0;
}

Output :

Upvotes: 3

Views: 25650

Answers (4)

Rand0m
Rand0m

Reputation: 372

I know this is a belated answer, but there is a complete and awesome library to allow you introduce your restful api via C/C++. Works on Linux, Freebsd, and Windows (probably more operating systems as well)

The github https://github.com/babelouest/ulfius

Upvotes: 0

asasasa
asasasa

Reputation: 1

Unlike SOAP-based web services, there is no "official" standard for RESTful web APIs. This is because REST is an architectural style, while SOAP is a protocol. Even though REST is not a standard per se, most RESTful implementations make use of standards such as HTTP, URI, JSON, and XML.

Upvotes: 0

JYOTI RANJAN PANDA
JYOTI RANJAN PANDA

Reputation: 122

I resolved the problem. Actually in RESTful POST method url and data should be added part by part. below is the successfull client in c for RESTfull POST method.

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;
  char url[]= "http://bulksms.net/API.svc/sms/json/sendmessage";
  char postData[] = "username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&dnRequired=false";
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  return 0;
}

Upvotes: 3

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25915

Your strcpy function cause undefined behavior because you have buffer of 80 char and source string is more then it.

Try something like this.

char url[]="http://bulksms.net/API.svc/sms/json/sendmessage?username=newuser&password=newpasswd&msg=test&msisdn=9999999999&tagname=Demo&shortcode=8888&telcoId=5&";

You can use strncpy or snprintf for safe copy and avoid undefined behaviour.

Upvotes: 1

Related Questions