예제 #1
0
int main(int argc, char *argv[]) {
    CURL *ch;                                               /* curl handle */
    CURLcode rcode;                                         /* curl result code */

    json_object *json;                                      /* json post body */
    enum json_tokener_error jerr = json_tokener_success;    /* json parse error */

    struct curl_fetch_st curl_fetch;                        /* curl fetch struct */
    struct curl_fetch_st *cf = &curl_fetch;                 /* pointer to fetch struct */
    struct curl_slist *headers = NULL;                      /* http headers to send with request */

    /* url to test site */
    char *url = "http://energybill.ir/api/remote";

    /* init curl handle */
    if ((ch = curl_easy_init()) == NULL) {
        /* log error */
        fprintf(stderr, "ERROR: Failed to create curl handle in fetch_session");
        /* return error */
        return 1;
    }

    /* set content type */
    headers = curl_slist_append(headers, "Accept: application/json");
    headers = curl_slist_append(headers, "Content-Type: application/json");

    /* create json object for post */
    json = json_object_new_object();

    /* build post data */
    //linux one: curl -H "Accept:application/json" -H "Content-Type: application/json" -X POST -d '{"SerialNumber":"61600010","AccumulatedEnergy":0.0,"AccumulatedVolume":0.0,"TariffEnergy":0.0,"FlowSensorVolume":0.0,"Power":0.0,"Aux1":0,"Aux2":0,"MeterTime":null,"OnTime":0,"ErrorTime":0,"LowTemp":0.0,"HighTemp":0.0,"ErrorCode":0}'  http://energybill.ir/api/remote
    //curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d
    //           curl -H "Accept:application/json" -H "Content-Type: application/json" -X POST -d '{"SerialNumber":"61600010","AccumulatedEnergy":10.6,"AccumulatedVolume":15.42,"TariffEnergy":0.0,"FlowSensorVolume":0.0,"Power":0.0,"Aux1":0,"Aux2":0,"MeterTime":null,"OnTime":0,"ErrorTime":0,"LowTemp":0.0,"HighTemp":0.0,"ErrorCode":0}'  http://energybill.ir/api/remote
    FILE *fptr;
    fptr=fopen("Room_Temperature.txt","r");
    char buffer[5];
    fgets (buffer,5,fptr);
    printf ("TT%s",buffer);

    FILE *fptrh;
    fptrh=fopen("Room_Humidity.txt","r");
    char bufferh[5];
    fgets (bufferh,5,fptrh);
    printf ("HH%s",bufferh);


    json_object_object_add(json, "SerialNumber", json_object_new_string("51600004"));
    json_object_object_add(json, "AccumulatedEnergy", json_object_new_int(0));
    json_object_object_add(json, "AccumulatedVolume", json_object_new_int(0));
    json_object_object_add(json, "TariffEnergy", json_object_new_int(0));
    json_object_object_add(json, "FlowSensorVolume", json_object_new_int(atoi(bufferh)));
    json_object_object_add(json, "Power", json_object_new_int(0));
    json_object_object_add(json, "Aux1", json_object_new_int(0));
    json_object_object_add(json, "Aux2", json_object_new_int(0));
    //json_object_object_add(json, "MeterTime", json_object_new_int((int32_t)));
    json_object_object_add(json, "OnTime", json_object_new_int(0));
    json_object_object_add(json, "ErrorTime", json_object_new_int(0));
    json_object_object_add(json, "LowTemp", json_object_new_double(atoi(bufferh)));
    json_object_object_add(json, "HighTemp", json_object_new_double(atof(buffer)));
    json_object_object_add(json, "ErrorCode", json_object_new_int(0));



    /* set curl options */
    curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(ch, CURLOPT_POSTFIELDS, json_object_to_json_string(json));

    /* fetch page and capture return code */
    rcode = curl_fetch_url(ch, url, cf);

    /* cleanup curl handle */
    curl_easy_cleanup(ch);

    /* free headers */
    curl_slist_free_all(headers);

    /* free json object */
    json_object_put(json);

    /* check return code */
    if (rcode != CURLE_OK || cf->size < 1) {
        /* log error */
        fprintf(stderr, "ERROR: Failed to fetch url (%s) - curl said: %s \n",
            url, curl_easy_strerror(rcode));
        /* return error */
        return 2;
    }

    /* check payload */
    if (cf->payload != NULL) {
        /* print result */
        printf("CURL Returned: \n%s\n", cf->payload);
        /* parse return */
        json = json_tokener_parse_verbose(cf->payload, &jerr);
        /* free payload */
        free(cf->payload);
    } else {
        /* error */
        fprintf(stderr, "ERROR: Failed to populate payload");
        /* free payload */
        free(cf->payload);
        /* return */
        return 3;
    }

    /* check error */
    if (jerr != json_tokener_success) {
        /* error */
        fprintf(stderr, "ERROR: Failed to parse json string\n");
        /* free json object */
        json_object_put(json);
        /* return */
        return 4;
    }

    /* debugging */
    printf("Parsed JSON: %s\n", json_object_to_json_string(json));

    /* exit */
    return 0;
}
예제 #2
0
int main(int argc, char *argv[]) {
	CURL *ch;                                               /* curl handle */
	CURLcode rcode;                                         /* curl result code */

	struct curl_fetch_st curl_fetch;                        /* curl fetch struct */
	struct curl_fetch_st *cf = &curl_fetch;                 /* pointer to fetch struct */
	struct curl_slist *headers = NULL;                      /* http headers to send with request */

	/* url to test site */
	std::string url = "localhost/wec/sample.php";

	/* init curl handle */
	if ((ch = curl_easy_init()) == NULL) {
		/* log error */
		fprintf(stderr, "ERROR: Failed to create curl handle in fetch_session");
		/* return error */
		return 1;
	}

	/* set content type */
	headers = curl_slist_append(headers, "Accept: application/json");
	headers = curl_slist_append(headers, "Content-Type: application/json");

	std::string json; 
	writeJson(json);

	/* set curl options */
	curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
	curl_easy_setopt(ch, CURLOPT_POSTFIELDS, json.c_str());

	/* fetch page and capture return code */
	rcode = curl_fetch_url(ch, url.c_str(), cf);

	/* cleanup curl handle */
	curl_easy_cleanup(ch);

	/* free headers */
	curl_slist_free_all(headers);

	/* check return code */
	if (rcode != CURLE_OK || cf->size < 1) {
		/* log error */
		fprintf(stderr, "ERROR: Failed to fetch url (%s) - curl said: %s",
				url, curl_easy_strerror(rcode));
		/* return error */
		return 2;
	}

	/* check payload */
	if (cf->payload != NULL) {
		/* print result */
		printf("CURL Returned: \n%s\n", cf->payload);
		/* parse return */
		free(cf->payload);
	} else {
		/* error */
		fprintf(stderr, "ERROR: Failed to populate payload");
		/* free payload */
		free(cf->payload);
		/* return */
		return 3;
	}

	/* exit */
	return 0;
}
예제 #3
0
파일: api.c 프로젝트: bspates/eg-shell
int json_request(char *method, char *url, struct json_object **body) {
  CURL *ch;    
  CURLcode rcode; // curl result code 

  struct curl_fetch_st curl_fetch; // curl fetch struct 
  struct curl_fetch_st *cf = &curl_fetch; // pointer to fetch struct 
  struct curl_slist *headers = NULL; // http headers to send with request

  /* init curl handle */
  if ((ch = curl_easy_init()) == NULL) {
      /* log error */
      fprintf(stderr, "ERROR: Failed to create curl handle in fetch_session");
      /* return error */
      return 1;
  }

  /* set content type */
  headers = curl_slist_append(headers, "Accept: application/json");
  headers = curl_slist_append(headers, "Content-Type: application/json");

  /* set curl options */
  curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, method);
  curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(ch, CURLOPT_POSTFIELDS, json_object_to_json_string(*body));

  /* fetch page and capture return code */
  rcode = curl_fetch_url(ch, url, cf);

  /* cleanup curl handle */
  curl_easy_cleanup(ch);

  /* free headers */
  curl_slist_free_all(headers);

   /* free json object */
  json_object_put(*body);

  /* check return code */
  if (rcode != CURLE_OK || cf->size < 1) {
      /* log error */
      fprintf(stderr, "ERROR: Failed to fetch url (%s) - curl said: %s",
          url, curl_easy_strerror(rcode));
      /* return error */
      return 2;
  }

  /* check payload */
  if (cf->payload != NULL) {
      *body = json_tokener_parse(cf->payload);
      /* free payload */
      free(cf->payload);
  } else {
      /* error */
      fprintf(stderr, "ERROR: Failed to populate payload");
      /* free payload */
      free(cf->payload);
      /* return */
      return 3;
  }
  return 0;
}