Languages

Menu
Sites
Language
HTTP Post Method in Tizen

Hello Everyone, 

I had a problems while parsing (Using  POST method) the data From server 

here is my code , please help 

 

void
get_http_data(const char* url, memoryStruct *data)
{
    CURL *curl_handle;
	CURLcode res;
	data->memory = malloc(1); /* will be grown as needed by the realloc above */
	data->size = 0; /* no data at this point */
	curl_global_init(CURL_GLOBAL_ALL);
	/* init the curl session */
	curl_handle = curl_easy_init();
	/* specify URL to get */
	curl_easy_setopt(curl_handle, CURLOPT_URL, url);
	/* send all data to this function */
	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_cb);

	/////
	struct curl_slist *list = NULL;
	list = curl_slist_append(list, "Content-Type: application/json");
	curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);

    //curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER,"Content-Type: application/json");

	curl_easy_setopt (curl_handle, CURLOPT_HTTPPOST, 1L);
    curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, "sign=aries&day=today");
	/////

	/* we pass our 'chunk' struct to the callback function */
	curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)data);
	/* some servers don't like requests that are made without a user-agent
	field, so we provide one */
	curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
	/* get it! */
	res = curl_easy_perform(curl_handle);
	/* cleanup curl stuff */
	curl_easy_cleanup(curl_handle);
	/* we're done with libcurl, so clean it up */
	curl_global_cleanup();
}


void Get_Horoscope_Response(void *data, char *Sign, char *Day)
{
    appdata_s *ad=data;

	//dlog_print(DLOG_ERROR, LOG_TAG, "Dinal *** 1");
	JsonParser *parser = json_parser_new ();

	char url[PATH_MAX];
	sprintf(url, "https://aztro.herokuapp.com");
	get_http_data(url,&ad->ms);

	elm_object_text_set(ad->label,ad->ms.memory);

	if(json_parser_load_from_data( parser,ad->ms.memory, strlen(ad->ms.memory), NULL))
	{
		JsonNode *root = json_parser_get_root(parser);
		JsonObject *obj = json_node_get_object(root);
		const char *compatibility = json_object_get_string_member(obj,"compatibility");

		dlog_print(DLOG_INFO, LOG_TAG, "Dinal compatibility : %s",compatibility);
	}
}

it sets " 'null' Try Again? Seems to be some kind of problem in the parameters :) " to my Entry ad->label

my Response seems to be in this format :

{"current_date": "June 23, 2017", "compatibility": " Cancer", "lucky_time": " 7am",
 "lucky_number": " 64", "color": " Spring Green", "date_range": "Mar 21 - Apr 20",
 "mood": " Relaxed", "description": "It's finally time for you to think about just
  one thing: what makes you happy. Fortunately, that happens to be a person who feels
  the same way. Give yourself the evening off. Refuse to be put in charge of anything."}

My Log :

Dinal compatibility : (null)

refrence link : https://aztro.readthedocs.io/en/latest/

Thank You, Dinal Jivani :D

Edited by: Dinal Jivani on 24 Jan, 2018
View Selected Answer

Responses

4 Replies
GEUNSOO KIM

let me check few things...

1. Is there any specific reason about using CURLOPT_HTTPPOST instead of CURLOPT_POST?  In my opinion, I cannot see any reason on yur code and CURLOPT_HTTPPOST is being deprecated from libcurl.

2. did you ever check the response code after the transaction? you can get it by using curl_easy_getinfo() with CURLINFO_RESPONSE_CODE option after curl_easy_perform() call.

3. and you'd better print out the return value of curl_easy_perform() call to check if there is any issue on the function call itself.

 

 

Dinal Jivani

I had already checked for the response, all are returning CURL_OK but to trim my code length to upload on forum I had removed ,

and about the CURLOPT_HTTPPOST i had used CURLOPT_POST but it was not sending any response so i had checked for the CURLOPT_HTTPPOST

but now when i use CURLOPT_POST there is no response in it, each function is returning CURL_OK. not getting my Exact mistake

I had used in following way : 

res = curl_easy_perform(curl_handle);
if(res == CURLE_OK)
dlog_print(DLOG_INFO, LOG_TAG, "Dinal curl_easy_perform SUCCESS !");

and this log is printed : 

Dinal curl_easy_perform SUCCESS !
GEUNSOO KIM

Ok, I got it. you set the following on CURLOPT_HTTPHEADER.

"Content-Type: application/json"

But your post body is "application/x-www-form-urlencoded" format.

And considering the reference site, you should not set the header for the POST.

just try after removing this line.

curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);

the line is not required. the server response will come with json format, but POST body shouldn't be json.

Mark as answer
Dinal Jivani

Yes , GEUNSOO KIM

So did I ,

here i removed all other stuff and passed my params in the url itself instead of using CURLOPT_POSTFIELDS , and finally got the success response

and set CURLOPT_POST active .

void
get_http_data(const char* url, memoryStruct *data)
{
    CURL *curl_handle;
	CURLcode res;
	data->memory = malloc(1); /* will be grown as needed by the realloc above */
	data->size = 0; /* no data at this point */
	res=curl_global_init(CURL_GLOBAL_ALL);

	curl_handle = curl_easy_init();
	curl_easy_setopt(curl_handle, CURLOPT_URL, url);
	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_cb);

	curl_easy_setopt (curl_handle, CURLOPT_POST, 1L);
	
	curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)data);
	curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

	res = curl_easy_perform(curl_handle);
    curl_easy_cleanup(curl_handle);
	curl_global_cleanup();
}

only thing I had to do was ;

curl_easy_setopt (curl_handle, CURLOPT_POST, 1L);

Thanks, I got it Clearly .