2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "cloud/http_request.h"
21 #include <curl/curl.h>
24 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data);
26 int http_request_get(const char *url, char **response, long *response_code)
28 retvm_if(!url, -1, "GET request URL is NULL!");
29 retvm_if(!response, -1, "GET request response is null");
32 CURLcode res = CURLE_OK;
34 curl = curl_easy_init();
35 retvm_if(!curl, -1, "Failed to initialize curl!");
37 curl_easy_setopt(curl, CURLOPT_URL, url);
38 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
39 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
41 res = curl_easy_perform(curl);
42 if (res != CURLE_OK) {
43 _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
44 curl_easy_cleanup(curl);
49 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
51 *response_code = _response_code;
54 curl_easy_cleanup(curl);
59 int http_request_post(const char *url, const char *json, char **response, long *response_code)
61 retvm_if(!url, -1, "POST request URL is NULL!");
62 retvm_if(!json, -1, "POST request JSON message is NULL!");
64 CURL *curl = curl_easy_init();
65 retvm_if(!curl, -1, "Failed to initialize curl!");
67 char *_response = NULL;
68 struct curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json");
70 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
71 curl_easy_setopt(curl, CURLOPT_URL, url);
72 curl_easy_setopt(curl, CURLOPT_POST, 1L);
73 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
74 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
75 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_response);
77 CURLcode res = CURLE_OK;
78 res = curl_easy_perform(curl);
79 if (res != CURLE_OK) {
80 _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
81 curl_slist_free_all(headers);
82 curl_easy_cleanup(curl);
87 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
89 *response_code = _response_code;
93 *response = _response;
99 curl_slist_free_all(headers);
100 curl_easy_cleanup(curl);
105 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
107 char **received = (char **)data;
108 size_t real_size = size * nmemb;
109 const char *response_msg = (const char *)ptr;
111 if (received && real_size > 0) {
113 char *temp = strndup(response_msg, real_size);
114 size_t length = strlen(*received) + strlen(temp);
115 char *new = g_malloc(length * sizeof(char));
116 snprintf(new, length, "%s%s", *received, temp);
122 *received = g_strndup((const char *)ptr, real_size);
126 _E("Failed to get response, response size : %lu", real_size);