MarkLogic version 8.0-4.2 on Windows 10
I'm sending a request to the /v1/eval endpoint to list the contents of a file system directory (xdmp:filesystem-directory) passing in the a directory path as a variable. The code works in the qconsole with xdmp:eval() however when using curl (see below) or powershell (using -Body with Invoke-RestMethod) I get the following error message when the external variable is accessed. The code I used is based on the example found at https://docs.marklogic.com/9.0/REST/POST/v1/eval
which works as expected.
Response:
...
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
...
{"errorResponse":{"statusCode":400, "status":"Bad Request", "messageCode":"XDMP-EXTVAR", "message":"XDMP-EXTVAR: (err:XPDY0002) declare variable $dpath as xs:string external; -- Undefined external variable fn:QName(\"\",\"dpath\")" ,"messageDetail":{"messageTitle":"Undefined external variable"}}}
Xquery example 1:
xquery=
xquery version "1.0-ml";
declare namespace dir="http://marklogic.com/xdmp/directory";
declare variable $dpath as xs:string external;
(string-join( fn:data(xdmp:filesystem-directory($dpath)//dir:pathname), ",") )
&
vars=("dpath":"d:\wrk\markLogic\")
Xquery example 2:
xquery=
xquery version "1.0-ml";
declare namespace dir="http://marklogic.com/xdmp/directory";
declare variable $dpath as xs:string external;
($dpath)
&
vars=("dpath":"abc")
Curl statement:
curl --anyauth --user user:password -X POST -i -d #./body.xqy -H "Content-type: application/x-www-form-urlencoded" -H "Accept: multipart/mixed; boundary=BOUNDARY" http://localhost:8000/v1/eval
Use curly braces, instead of round parentheses:
vars={"dpath":"d:/wrk/markLogic/"}
You probably also need to escape characters like \ and &. Given the syntax, it probably follows JSON notation requirements.
HTH!
Related
I need help because I'm not good with HTTP Requests.
I am trying to create a Release on my XL Release server with a HTTP request. Right now I'm doing it with Curl, in a batch file, like that
curl "https://{ID}:{password}#{IP}:{port}/api/v1/templates/Applications/Folder{IDFolder}/create" -i -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d %0\..\ReleaseConfig.json
The data file, which is in the same directory as the script, I'm calling is a json like that :
{ "releaseTitle" : "API Test", "releaseVariables" : { }, "releasePasswordVariables" : { }, "scheduledStartDate" : null, "autoStart" : false }
The problem is, I get an error like that while executing my command :
RESTEASY003065: Cannot consume content type
Do you have any idea what can help my case ?
Thanks
By looking at your filename, it seems that you are on Windows. I suspect you can't escape your Content-type with quote, you have to use double quotes.
Also, to pass a file as POST data, you have to use an #, like this:
curl "https://{ID}:{password}#{IP}:{port}/api/v1/templates/Applications/Folder{IDFolder}/create" -i -X POST -H "Content-Type:application/json" -H "Accept:application/json" -d #%0\..\ReleaseConfig.json
I am working on providing support for sending http post requests from one of our tools. The tool basically executes a job via http requests.
The way this is achieved is the tool makes a call to RunScript.bat with a number of arguments. The script parses these args and makes a curl post request after validation. The Post request is handled by our internal Java Service.
What I want is my batch file should be able to fetch the Response Code (200/409 etc.) of the post request and based on this, the script should return 1 or 0 as exit value back to the tool.
The tool then marks the job pass/fail based on the return value.
Tool: Parses the returned value
RunScript.bat arg1 arg2 arg3
RunScript.bat
#echo off
SETLOCAL
ECHO %DATE% %TIME% Calling %0 with %*
SET "SCRIPT_DIR=%~dp0"
ECHO Script Dir %SCRIPT_DIR%
SET cmd=%1
SET val2=%2
SET val3=%3
%SCRIPT_DIR%\curl -v -X POST http://localhost:9500/%cmd%/%val2%/%val3%
Is it possible to fetch the Response Code of the curl request in the batch script and then return 1/0 based on the Response Code ?
The first thing we need is to get curl to output the HTTP status. For this I used pvandenberk's answer to Getting curl to output HTTP status code?. Next, we have to get that output into an environment variable so we can test it. For this we use the for /f %%a in ( 'command-to-execute' ) do ... form of the for command (see for /? for more details).
Incorporating these (and some other minor tweaks), something like the following should help:
#echo off
SETLOCAL
ECHO %DATE% %TIME% Calling %0 with %*
SET "SCRIPT_DIR=%~dp0"
ECHO Script Dir %SCRIPT_DIR%
SET "cmd=%1"
SET "val2=%2"
SET "val3=%3"
SET "URL=http://localhost:9500/%cmd%/%val2%/%val3%"
SET HTTP=
for /f %%a in ( '"%SCRIPT_DIR%\curl" -s -o nul -v -X POST -w "%%{http_code}" "%URL%"' ) do set HTTP=%%a
if "%HTTP%" == "200" (
exit /b 0
) else (
exit /b 1
)
Notes:
I've assumed cmd, val2 and val3 in your original URL were meant to be replaced with the contents of the earlier environment variables, so have changed them to %cmd% etc. when building URL.
In the for statement, the whole of the command to be executed is enclosed in single-quotes ('.....').
I've wrapped the invocation of curl in double-quotes ("%SCRIPT_DIR%\curl"): this should allow it to work even if the script-directory were to contain spaces.
I've added -s (silent) and -o nul (where to send the full response) to curl's command-line. This will help to ensure any output returned from the POST request does not interfere with the HTTP status code. If you need to see this output, use -o filename and TYPE the file before making the test on HTTP.
The percent-sign in -w "%%{http_code}" needs to be doubled because it has special meaning to the shell.
The output of -v (verbose) is sent to stderr so shouldn't interfere with capturing the HTTP response code.
Update
To get both the HTTP response code, and still see the output from the curl command itself you could try replacing the for /f ... line above with the one below:
for /f "delims=" %%a in ( '"%SCRIPT_DIR%\curl" -v -X POST -w "%%{http_code}" "%URL%"' ) do set "HTTP=%%a" && echo %%a
The "delims=" means that the lines of output from curl will not be split into separate tokens so %%a will hold the whole of each line.
I've removed the -s and -o nul options, so the original output from curl will still be generated.
The set "HTTP=%%a" command has had double-quotes added to prevent trailing spaces being added to the environment variable. Because the response code (e.g. 404) is the last line emitted by curl, the final value of HTTP will still be what we want.
The echo %%a will replay all lines generated by curl.
I need to write all the curl arguments in a separate file(myconfig.txt) to be executed and output for each different lines of command in output.txt, output1.txt, output2.txt...so on.
The webservices should be requested with post (multipart/form-data) method.
I am trying this command curl -K myconfig.txt -o output.txt
contents of
myconfig.txt include URL="http://1x2.2x1.x6.1x2:3000/latto/get_notifications"<option="to";mobile="+91999xx6xx3x"> into the output.txt - 'Cannot get /latto/get_notifications'. It seems like web-services are being requested as get method. Please anyone tell me the syntax to write in myconfig file. So that I can get the right output in file.
However, When I run this command sudo curl --form option="to" --form mobile="+9199999yyyxx" 1x2.2x1.x6.1x2:3000/latto/get_notifications, output is successfully printed in terminal.
When I do a curl -k config.txt with config.txt containing this:
URL="http://my.test.domain/index.php"
-d option="to"
-d mobile="+91999xx6xx3x"
I get the result:
POST
array(2) {
["option"]=>
string(4) ""to""
["mobile"]=>
string(15) "" 91999xx6xx3x""
}
So I don't known what you mean with <option ...> but when you put it into your config file as stated above, it should work.
Btw. here is the php script that answered:
<?php
echo $_SERVER['REQUEST_METHOD']."\n";
var_dump($_REQUEST);
?>
This webservice accepts request in POST method and enctype multipart/form-data
URL="http://my.test.domain/get_notifications"
-F option=to
-F mobile=+91999xx6xx3x
After a lot of hit and trial, I got this working: curl -k config.txt with config.txt containing the above.
I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.
curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_file"
You're looking for the --data-binary argument:
curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "#path/to/file"
In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an # symbol, so curl knows to read from a file.
I need to make a POST request via Curl from the command line. Data for this request is located in a file...
All you need to do is have the --data argument start with a #:
curl -H "Content-Type: text/xml" --data "#path_of_file" host:port/post-file-path
For example, if you have the data in a file called stuff.xml then you would do something like:
curl -H "Content-Type: text/xml" --data "#stuff.xml" host:port/post-file-path
The stuff.xml filename can be replaced with a relative or full path to the file: #../xml/stuff.xml, #/var/tmp/stuff.xml, ...
If you are using form data to upload file,in which a parameter name must be specified , you can use:
curl -X POST -i -F "parametername=#filename" -F "additional_parm=param2" host:port/xxx
Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file (XLSX spread sheet) using POST method, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.
curl -POST -F comment=mycomment -F file_type=XLSX -F file_data=#/your/path/to/file.XLSX http://yourhost.example.com/api/example_url
I was having a similar issue in passing the file as a param. Using -F allowed the file to be passed as form data, but the content type of the file was application/octet-stream. My endpoint was expecting text/csv.
You are able to set the MIME type of the file with the following syntax:
-F 'file=#path/to/file;type=<MIME_TYPE>
So the full cURL command would look like this for a CSV file:
curl -X POST -F 'file=#path/to/file.csv;type=text/csv' https://test.com
There is good documentation on this and other options here: https://catonmat.net/cookbooks/curl/make-post-request#post-form-data
I had to use a HTTP connection, because on HTTPS there is default file size limit.
https://techcommunity.microsoft.com/t5/IIS-Support-Blog/Solution-for-Request-Entity-Too-Large-error/ba-p/501134
curl -i -X 'POST' -F 'file=#/home/testeincremental.xlsx' 'http://example.com/upload.aspx?user=example&password=example123&type=XLSX'
Using REST Server 6.x-2.0-beta3, I'm trying to understand how to post to user.save.
curl -d 'XX' -v http://localhost/services/rest/service_user/save
I've tried to replace XX with:
account{'name':'myname','pass':'mypassword','mail':'my#email.org'}
account = {'name':'myname','pass':'mypassword','mail':'my#email.org'}
account="name=myname,pass=mypassword,mail=myemail.org"
account=name=myname,pass=mypassword,mail=myemail.org
account=myname,mypassword,myemail.org
But none of these seems to be right and finding any documention regarding this is next to impossible.
I've also tried the following:
curl -H "Content-Type: application/json" -d 'account={"name":"myname","pass":"mypassword","email":"123"}' -v http://localhost/services/rest/service_user/save
The error I get in this case is:
HTTP/1.0 406 Not Acceptable: Missing required argument account
Hi I also just started working with this module and wondering how to create content using JSON.
Just been able to create a simple node using this:
Post URL: http://path-to-site/services/rest/node
Request Header: Content-Type: application/json
Request Body: {"type":"story","title":"REST Test","body":"REST using JSON"}
I think you're using the wrong URL
I figured it out:
curl -H “application/x-www-form-urlencoded” -d "sessid=xxxx" -d "account[name]=MyName&account[pass]=mypass&account[mail]=myemail#gmail.com&account[conf_mail]=myemail#gmail.com" -v http://path-to-site/services/rest/service_user/save
You only have to add -d "sessid=xxxx" if you have configured Services to require a session. Make sure in that case to replace xxxx with your actual session id (from system.connect).