c++ - "Failed writing body" CURLOPT_WRITEDATA -
the below code response server using wsdl, here problem curl returns response unable print it.
error:
failed writing body failed writing data
#include<stdio.h> #include<string.h> #include"../include/curl.h" size_t write_data(void *ptr, size_t size, size_t count, void *stream) { /* ptr - string variable. stream - data chuck received */ printf("%.*s", size, (char*)stream); } int main() { int res=0,i=0; char buffer[4098]="",buff[128]="",buf[256]="",buf7[30]="",buf6[30]="",buf5[30]=""; char machineid[]="subani"; char filename1[50]=""; int refno=0,paymode=0,taxtype=0; file *fbc; memset(filename1,0,sizeof(filename1)); sprintf(filename1,"/mnt/jffs2/response_details1.xml"); lk_dispclr(); lk_disptext(1,0,(unsigned char *)"sending request",0); lk_disptext(2,0,(unsigned char *)"please wait",0); memset(buffer,0,sizeof(buffer)); sprintf(buffer,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\ <soap:envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:log=\"http://wsdlclassess.application.sims.test.com\">\ <soap:header>\ </soap:header>\ <soap:body>\ <log:loginmethod>\ <log:loginid>%s</log:loginid>\ <log:password>%s</log:password>\ </log:loginmethod>\ </soap:body>\ </soap:envelope>","raja","test"); res=get_file1(buffer,filename1); return 0; } int get_file1(char *buffer,char *filename) { curl *curl; curlcode res; struct curl_slist *headers = null; file *out_fd = (file *) 0; char errorbuf[300] = "",tmpbuff[128]=""; char errmsg[256]; int timeout=120; //default timeout = 2 mins int buffer_size = 0; char urlbuff[256]=""; char mstr[10240]; memset(urlbuff,0,sizeof(urlbuff)); memset(tmpbuff,0,sizeof(tmpbuff)); buffer_size = strlen(buffer); strcpy(tmpbuff,"http://10.10.1.111:8081/test_server/services/application?wsdl"); tmpbuff[strlen(tmpbuff)]='\0'; curl = curl_easy_init(); if(curl) { out_fd = fopen (filename, "w"); curl_easy_setopt(curl, curlopt_file, out_fd); printf("%s:sign-in request\n", __func__); headers = curl_slist_append(headers, "content-type:application/soap+xml; charset=utf-8; action=\"http://wsdlclassess.application.sims.test.com/loginmethod\""); curl_easy_setopt(curl, curlopt_url, tmpbuff); curl_easy_setopt(curl, curlopt_noprogress, 0); curl_easy_setopt(curl, curlopt_verbose, 1); curl_easy_setopt(curl, curlopt_httpheader, headers); curl_easy_setopt(curl, curlopt_writefunction, write_data); curl_easy_setopt(curl, curlopt_writedata, mstr); curl_easy_setopt(curl, curlopt_postfieldsize, buffer_size); curl_easy_setopt(curl, curlopt_postfields, buffer); curl_easy_setopt(curl, curlopt_verbose, 1l); curl_easy_setopt(curl, curlopt_timeout, timeout); curl_easy_setopt(curl, curlopt_errorbuffer,errmsg); printf("the server%s:performing transaction.....\n",__func__); res = curl_easy_perform(curl); printf("res=after culreasey perform%d\n",res); curl_slist_free_all(headers); curl_easy_cleanup(curl); printf("\nerrorbuf:%s\n",errmsg); fclose(out_fd); if(curle_ok != res) { puts("error occured is\n" ); //ppp_close(); return -1; } } return 0; }
the error don't return correct value function, in fact don't return anything.
also, data provided function first ptr
argument.
i agree documentation not clear, says:
the size of the data pointed ptr size multiplied nmemb, not 0 terminated.
the above line (emphasis mine) tells data in ptr
first argument in function declaration provided in documentation.
the documentation also states:
return number of bytes taken care of. if amount differs amount passed function, it'll signal error library. abort transfer , return
curle_write_error
.
you don't return value function, , have undefined behavior seemingly random value being returned causing whole operation fail. fix should return size * count
.
you uses size
print string, size of underlying type used (probably 1
), count
variable number of characters read curl. working, without invoking more undefined behavior (since data not terminated) should call printf
like:
printf("%*.*s", size * count, size * count, ptr);
Comments
Post a Comment