0

I am using cURL to fetch information from servers. This is the query URL I get "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)" When this is directly hit through browser I get proper response as below

{
   "md5": "c22f2d0c39cb6c9f15c170fbedfab634",
   "original": "Fri Jun 13 2014 07:41:27 GMT 0300 (EEST)"
}

But When I try it with cURL example i get 406 error. Below is my sample code

int main(void)
{
  CURL *curl;
  CURLcode res;
int data2len;
char* temp = "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";

  curl = curl_easy_init();
  if(curl) {

    curl_easy_setopt(curl, CURLOPT_URL,temp );
    /* example.com is redirected, so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */ 
   res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

Kindly help !! Thanks in advance.

swati
  • 129
  • 1
  • 18

1 Answers1

0

Only the parameter values should be encoded.you do it 2 ways.

one way is replace

char* temp = "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";

with

char* temp = "http://md5.jsontest.com/?text=Fri%20Jun%2013%202014%2007:41:27%20GMT+0300%20%28EEST%29";

other way is using the curl_easy_escape method.

int data2len;
char temp[1024]="";
strcat(temp,"http://md5.jsontest.com/?text=");
char* param1="Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";
curl = curl_easy_init();
if(curl) {
    strcat(temp,curl_easy_escape(curl,param1,strlen(param1)));
    curl_easy_setopt(curl, CURLOPT_URL,temp ); 

Thanks and Regards.

RadhaKrishna
  • 312
  • 3
  • 13