curl request through c++ for indexing solr document - c++

I have used following code for indexing document in solr
CURL *curl = curl_easy_init();
CURLcode res;
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.164:8983/solr/collection1/update?replacefields=false -H 'Content-type: application/json' -d '[{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc\":\"unknown \", \"subject\":\"Life\"}]'");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
if(CURLE_OK == res){
logger.LogError("res value CURLE_OK");
}
/* always cleanup */
curl_easy_cleanup(curl);
}
and the return value of curl_easy_perform(curl) i.e. res is CURLE_OK but the record is not indexing in collection1 of solr and while posting following command from terminal record is getting indexed
curl http://192.168.0.164:8983/solr/collection1/update?replacefields=false -H 'Content-type: application/json' -d '[{"id":"4000", "to":"Life is to.", "cc":"unknown ", "subject":"Life"}]'

You're not supplying cURL with your proper URL. Everything after the actual URL, -H 'Content-type: application/json' -d '[{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc\":\"unknown \", \"subject\":\"Life\"}]', is just arguments to the cURL command line tool, and is not part of the URL.
The URL option should be only http://192.168.0.164:8983/solr/collection1/update?replacefields=false. The rest of the parameters has to be set as their own curl_easy_setopt calls.
To set the data to POST, use CURLOPT_POSTFIELDS.
Pass a char * as parameter, pointing to the full data to send in a HTTP POST operation. You must make sure that the data is formatted the way you want the server to receive it.
To set the proper request content type, use CURLOPT_HTTPHEADER.
Pass a pointer to a linked list of HTTP headers to pass to the server and/or proxy in your HTTP request. The same list can be used for both host and proxy requests!
After setting all the options call curl_easy_perform. You may also want to watch the log on the Solr server to see if Solr generates an exception. You can also set CURLOPT_ERRORBUFFER and CURLOPT_VERBOSE to get more information about any failure internally in cURL (CURLE_OK will be returned as long as cURL is able to make the request - but does not change if the server returns 400 or 404 or any actual error code on the server side (except if CURLOPT_FAILONERROR is set).

Related

How can I pass args to a curl request executed via boost::process:child?

I'm able to execute a http POST request using curl via boost::process::child by passing the entire command line. However, I would like to pass the arguments via boost::process::args but I cannot get it work.
This works:
const std::string cmdDiscord = "curl -X POST https://discord.com:443/api/webhooks/1234567890 -H \"content-type: application/json\" -d \"{\"content\": \"test\"}\"";
boost::process::child c(cmdDiscord); // this works
boost::process::child c(boost::process::cmd = cmdDiscord); // strangely, this doesn't work
I want to use boost::process::args but this fails:
std::vector<std::string> argsDiscord {"-X POST",
"https://discord.com:443/api/webhooks/1234567890",
"-H \"content-type: application/json\"",
"-d \"{\"content\": \"test\"}\""};
boost::process::child c(boost::process::search_path("curl"), boost::process::args (argsDiscord));
The error is curl: (55) Failed sending HTTP POST request which is quite a vague error message. I couldn't find any examples calling curl. Does anyone have any suggestions on getting this to work?
It should be
std::vector<std::string> argsDiscord {"-X", "POST",
"https://discord.com:443/api/webhooks/1234567890",
"-H", "content-type: application/json",
"-d", "{\"content\": \"test\"}"};
Since command interpretators pass arguments like -X POST are two arguments, not one.
The double quotes are a shell syntax as well. The shell interprets (removes) them during command line expansion.
Alternatively curl accepts adjacent values in short options (without space)
std::vector<std::string> argsDiscord {"-XPOST",
"https://discord.com:443/api/webhooks/1234567890",
"-H", "content-type: application/json",
"-d", "{\"content\": \"test\"}"};

Which `format` would be negotiated for REST request?

There are three variants of format selection:
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.json"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser?format=json"
curl -uadmin:admin -H "Accept: text/html" "http://localhost:8080/alfresco/service/hellouser"
But this is unclear from the DOC what format would be selected for next query:
curl -uadmin:admin -H "Accept: text/html" "http://localhost:8080/alfresco/service/hellouser.xml?format=json"
I expect json here.
May someone provide links to relevant specifications or documentation which describes priority how {format} negotiated? like this is described for Rails:
Rails picks up the expected format from the query parameter format, or if not there from the URL path suffix, or it not there from the Accept header
UPD
The controller can handle all supplied formats: json, xml, html
UPD
Another corner case:
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.pl?format=json"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.pl?format=xml"
I'd believe you wouldn't have a 200 response, only an error with content negotiation.
The code shows that:
?format=json(format_query_param) will be discarded by the .xml (format_suffix)
filter available renderers leaving only the XMLRenderer left
then it will loop on the accept header but none will match text/html
finally this will be down to the exception

WSO2DSS - REST POST operation call in JSON format : exception

Followed instructions in the below to send the json request payload in the curl command
https://docs.wso2.com/display/DSS351/JSON+Mapping+Sample.
Below is the curl command:
D:\testing\curl-7.46.0-win64\bin>curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data "#article-payload.json" http://8.39.51.27:9763/services/KMTool/insertarticles
The error:
**Error: Getting below error:**
Warning: Couldn't read data from file "article-payload.json", this makes an
Warning: empty POST.
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: application
<axis2ns22:DataServiceFault xmlns:axis2ns22="http://ws.wso2.org/dataservice"><ax
is2ns22:current_params>{articleTitle=null, articlePath=null, articleContent=null
, articleDesc=null, categoryID=null}</axis2ns22:current_params><axis2ns22:source
_data_service><axis2ns22:data_service_name>KMTool</axis2ns22:data_service_name><
axis2ns22:description>N/A</axis2ns22:description><axis2ns22:location>\KMTool.dbs
</axis2ns22:location><axis2ns22:default_namespace>http://ws.wso2.org/dataservice
</axis2ns22:default_namespace></axis2ns22:source_data_service><axis2ns22:ds_code
>DATABASE_ERROR</axis2ns22:ds_code><axis2ns22:nested_exception>org.postgresql.ut
il.PSQLException: ERROR: null value in column "articleTitle" violates not-null c
onstraint
Detail: Failing row contains (31, null, null, null, null, null).</axis2ns22:ne
sted_exception><axis2ns22:current_request_name>_postinsertarticles</axis2ns22:cu
rrent_request_name></axis2ns22:DataServiceFault>
I saved the file under below location:
C:\Users\testUser\Downloads\wso2dss-3.5.1\repository\deployment\server\dataservices\article-payload.json
Not sure what is causing issue. Just followed the instructions provided by wso2dss documentation.
Seems like the parameters are not passed correctly (ex: articleTitle has become null, but in the database table you have defined it as a not null column). The article-payload.json file should be located in the directory that you are trying to invoke the request using curl (ie. inside D:\testing\curl-7.46.0-win64\bin)

posting a parameter with integer value using requests lib

I have a curl command that was given to me that I have to convert using requests.
curl --request POST "https://www.example.com" --data "user_id=200" --data "user_data=je93jfe92dj220,39fjid20djd93f302,93jfieheio02hfne,902jfoienfieshiu202" --header "Authorization: Bearer [TOKEN]"
using requests, the call should be
hdr = {'Content-Type': 'Content-type: application/json',
'Authorization': 'Bearer TOKEN' }
payload = {"user_id":200,"records":"je93jfe92dj220,39fjid20djd93f302,93jfieheio02hfne,902jfoienfieshiu202"
requests.post('https://www.example.com', headers=hdr, data=json.dumps(payload))
This isn't working as I'm getting an error returned that the 'user_id' param must be an integer. Not sure how to ensure that, as aren't all parameters formatted as strings when sent? The curl command does work, however.
Im not really sure the reason, but to make this work, just change data=json.dumps(payload) to json=json.dumps(payload). This worked just fine for me.

Not able to handle multiple request by Django

I am trying to send multiple json object in one request , but I am not able to extract values from it.
def config(request):
print "type",type(request)
print "request in config",request.body
if request.method == 'POST':
print "In Handler"
print "Request Body is ",request.body
data = simplejson.loads(request.body)
for i in data:
print "REQUEST is ____",i
z = json.dumps(i)
rew = retResp(z)
proID = request.data["proId"]
For request 1 can find proID but for request 2 I am not able to get proID.
req1 = curl -X POST http://192.168.2.242:8080/server/config/ -d '{"confMsgReqId": 2,"proId":"123"}' -H "Content-Type: application/json"
req2 = curl -X POST http://192.168.2.242:8080/server/config/ -d '[{"confMsgReqId": 2,"proId":"123"},{"confMsgReqId": 2,"proId":"345"}]' -H "Content-Type: application/json"
The data in the first request is a dictionary, so you can access data['proID'].
The data in the second request is a list of two dictionaries, so you can access data[0]['proID'] or data[1]['proID'].
You'll find it easier if you use an array for the first request as well as the second. It's possible to make an api that can handle a single element of a list, but it makes the code more complicated.
req1 = curl -X POST http://192.168.2.242:8080/server/config/ -d '[{"confMsgReqId": 2,"proId":"123"}]' -H "Content-Type: application/json"
Looking at your code, you might want to access the id inside the loop. It's not clear why you are decoding the json manually instead of using request.data.
for d in request.data:
d["proId"]