How create WMS layer in GeoServer by REST API? - postgresql

In PostgreSQL I have a scheme called geo. Inside that scheme, I have a table with a column which has a geometry data type.
I am new in GeoServer and want to know how to create WMS layer by REST API with data of that remote PostgreSQL database? According to the documentation I need to create workspace and datastore first, right? I'm a little confused. What sequence of actions should be? I will be grateful for any example!
Result of curl request:

The REST API works in exactly the same manner as the GUI, so the process is that you can optionally create a new workspace or use an existing one, you then create a store inside a workspace and then create layers from the store. Any layer will automatically become available as a WMS layer.
Create a new PostGIS store, generate a file with the connection details:
<dataStore>
<name>nyc</name>
<connectionParameters>
<host>localhost</host>
<port>5432</port>
<database>nyc</database>
<user>bob</user>
<passwd>postgres</passwd>
<dbtype>postgis</dbtype>
</connectionParameters>
</dataStore>
and POST it to the REST endpoint
curl -v -u admin:geoserver -XPOST -T <file.xml> -H "Content-type: text/xml"
http://localhost:8080/geoserver/rest/workspaces/<WORKSPACENAME>/datastores
Then publish the table as a layer
curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<featureType><name>buildings</name></featureType>" http://localhost:8080/geoserver/rest/workspaces/acme/datastores/nyc/featuretypes

optional.
Publishing a table from an existing PostGIS store.
on php curl.
function curl_post($url,$params) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD,"user:password");//--> on geoserver.
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Receive server response ...
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
//- table_name : xxx
//- work_space_name : your_workspace
//- store_name : dbtest
$layer_name = "xxx";
$params ="
<featureType>
<name>".$layer_name."</name>
<nativeName>".$layer_name."</nativeName>
<title>".$layer_name."</title>
<keywords>
<string>".$layer_name."</string>
<string>features</string>
</keywords>
<srs>EPSG:3857</srs>
<nativeBoundingBox>
<minx>1.0836244E7</minx>
<maxx>1.1759454E7</maxx>
<miny>625842.375</miny>
<maxy>2328151.75</maxy>
</nativeBoundingBox>
<latLonBoundingBox>
<minx>97.34363607648459</minx>
<maxx>105.63697261100442</maxx>
<miny>5.613037739416236</miny>
<maxy>20.464604971116074</maxy>
</latLonBoundingBox>
<projectionPolicy>FORCE_DECLARED</projectionPolicy>
<enabled>true</enabled>
<metadata>
<entry key=\"cachingEnabled\">false</entry>
</metadata>
<maxFeatures>0</maxFeatures>
<numDecimals>0</numDecimals>
</featureType>
";
$str_url = "http://xxx.co.uk/geoserver/rest/workspaces/**your_workspace**/datastores/**your_data_store**/featuretypes";
$rs = curl_post($str_url, $params);

Related

Use groovy-wslite make a post request from CURL request

Hi i have the following CURL request that i'd like to include in a groovy script using the groovy-wslite library put struggling to get the request working.
curl -s -X POST -k -u user:password https://artifactory_url/api/search/aql -H 'Content-Type: text/plain' -d 'items.find({"type":"file","repo":{"$eq": "my-repo-name"},"path":{"$match":"com/mycompany/product1/subcat/mob/*"},"name":{"$match":"*apk"}}).sort({"$desc":["path"]}).limit(1)'
You can use http-builder-ng and your code could look like
compile 'io.github.http-builder-ng:http-builder-ng-CLIENT:1.0.4'
HttpBuilder.configure {
request.uri = 'https://artifactory_url/api/search/aql'
request.auth.basic 'un', 'pw'
request.contentType = 'text/plain'
}.post {
request.body = 'items.find({"type":"file","repo":{"$eq": "my-repo-name"},"path":{"$match":"com/mycompany/product1/subcat/mob/*"},"name":{"$match":"*apk"}}).sort({"$desc":["path"]}).limit(1)'
response.success { FromServer fs, Object body ->
println body
}
}

Translating curl into Matlab/Webwrite

I have the following curl command I need to sent to a web server using Matlab and webwrite using POST. My problem is that I always get a "Bad request" answer so my syntax must be wrong somehow. Does anybody have an idea how this curl command, sending the body could look like in Matlab using webwrite in a correct way ?
body=$(cat << EOF
{
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
-d "$body" \
"https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders"
I have just asked a potentially similar question so this may not work first time. However I cannot test without knowing some login details so I can but hope this helps.
data_InputValues = struct ('units',100,'instrument','EUR_USD','timeInForce','FOK','type','MARKET','positionFill','DEFAULT');
MyBody = matlab.net.http.MessageBody(struct('order',data_InputValues));
MyHTTPOptions = matlab.net.http.HTTPOptions(); % use this to change the options if necessary (e.g. extend timeout)
Request = matlab.net.http.RequestMessage;
Request.Method = 'POST';
Request.Header = matlab.net.http.HeaderField('Content-Type','application/json','Authorization: Bearer',AUTHENTICATION TOKEN);
Request.Body = MyBody;
uri = matlab.net.URI('https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders');
[response a ~] = Request.send(uri,MyHTTPOptions);
The part I struggle with is generating the MyBody part (in your case this is parsing the order variable's sub-variables). If you get this to work I would be keen to know how! P.S. my question in case it helps: Matlab RESTful PUT Command - net.http - nesting body values
The correct format for the body is as follows:
body = struct('units',100,'instrument','EUR_USD','timeInForce','FOK',...
'type','MARKET','positionFill','DEFAULT');
As for the HTTP headers that you require you can specify them with weboptions when using webwrite.
The syntax for an additional header:
options = weboptions('KeyName','Name','KeyValue','Value')
Where Name and Value are the name of the header and its value respectively.
You must add the headers that you require in weboptions.
For the code you provided, the correct syntax would be as follows:
options = weboptions('MediaType','application/json',...
'KeyName','Authorization: Bearer','KeyValue','Token');
You can then perform the POST request at the URL of interest.
response = webwrite(url,body,options);

Can't able to view a transform in browser using REST in Marklogic 9?

I tried to install below Server-Side JavaScript using this documentation and saved below as rest-sjs
function insertTimestamp(context, params, content)
{
if (context.inputType.search('json') >= 0) {
var result = content.toObject();
if (context.acceptTypes) { /* read */
result.readTimestamp = fn.currentDateTime();
} else { /* write */
result.writeTimestamp = fn.currentDateTime();
}
return result;
} else {
/* Pass thru for non-JSON documents */
return content;
}
};
exports.transform = insertTimestamp;
I tried to push this using below curl cmd:
curl --anyauth --user public\admin:admin -X PUT -i --data-binary #"C:/Users/name/Desktop/rest.sjs" -H "Content-type: application/vnd.marklogic-javascript" 'http://localhost:9963/v1/config/transforms/js-example'
When I used localhost:9963 and went to /v1/config/transforms I can see:
<rapi:transforms xmlns:rapi="http://marklogic.com/rest-api">
<rapi:transform>
<rapi:name>rest-tsm</rapi:name>
<rapi:source-format>javascript</rapi:source-format>
<rapi:transform-parameters/>
<rapi:transform-source>/v1/config/transforms/rest-tsm</rapi:transform-source>
</rapi:transform>
</rapi:transforms>
But when I went though the module /v1/config/transforms/rest-tsm I am seeing an error response:
<error-response xmlns="http://marklogic.com/xdmp/error">
<status-code>406</status-code>
<status>Unacceptable Type</status>
<message-code>REST-UNACCEPTABLETYPE</message-code>
<message>
REST-UNACCEPTABLETYPE: (err:FOER0000) No acceptable content type: None of the requested types text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 can be provided
</message>
</error-response>
I can see the the module in Modules db. Which worked fine when I try to insert a document by using the transform.
Why can't I view the transform in the browser?
Unfortunately, that rest endpoint isn't very browser friendly. The required/acceptable Accept header values do not match what browsers will normally send.
When you made the GET request through your browser, it was sending the following Accept header:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept
This field contains a semicolon-separated list of representation schemes ( Content-Type metainformation values) which will be accepted in the response to this request.
Unfortunately, the v1/config/transform/{name} (GET) REST endpoint is strict in what it will accept for the Accept header and expects a specific value:
The MIME type of the data expected in the response, either application/xslt+xml or application/xquery.
If you use the example CURL command from the documentation, and customize for your transform URI, it will return the expected response.
curl --anyauth --user public\admin:admin -X GET -i \
-H "Accept: application/xquery" \
http://localhost:9963/v1/config/transforms/rest-tsm

Error Paypal REST API (sandbox) 404:"The requested URL /cgi-bin/ppapi was not found on this server."

I'm using PHP curl and it is communicating with the server. I'm trying to get a token. I get the following unhelpful response from the paypal sandbox:
Not Found
The requested URL /cgi-bin/ppapi was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache Server at api.sandbox.paypal.com Port 443
I'm running the code (below) from localhost on my laptop.
/gettoken.php
<?php
//header('Content-Type:application/javascript');
$url = "https://api.sandbox.paypal.com/token";
$client_id = "xxx";
$secret = "xxx";
// Initiate curl
$ch = curl_init();
// set basic auth
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
// set user/password
curl_setopt($ch,CURLOPT_USERPWD,$client_id.":".$secret);
// set header accept
$headers = array('Accept:application/json','Accept-Language:en_US');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
// set mode to POST
curl_setopt($ch,CURLOPT_POST,1);
// add post fields (1)
$fields_string = "grant_type=client_credentials";
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
// Disable SSL verification
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
// close curl
curl_close($ch);
// decode result
//var json = json_decode($result, true);
echo('<pre>'.$result.'</pre>');
?>
My apologies, the url to the REST service was wrong.. should have been:
https://api.sandbox.paypal.com/v1/oauth2/token
At least we know that if you get this error that it is likely the REST endpoint url is probably wrong.

How to use crocdoc in php

I am using crocdoc in php
https://github.com/crocodoc/crocodoc-php
i downloaded the files in my local i had my API TOKEN so i pasted it there but it doesnt seems to work and throws error shown below for all 15 examples
can any one help me here
This is the error here which i am getting on using above github code with my Token
Example #1 - Upload Form W4 from the IRS by URL.
Uploading... failed :(
Error Code: curl_exception
Error Message: Crocodoc: [curl_exception] Crocodoc::_request
{"curl_errno":60,"curl_error":"SSL certificate problem, verify that the CA cert is OK. Details:\nerror:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed"}
Example #15 - Delete the second file we uploaded.
Deleting... failed :(
Error Code: curl_exception
Error Message: Crocodoc: [curl_exception] Crocodoc::_request
{"curl_errno":60,"curl_error":"SSL certificate problem, verify that the CA cert is OK. Details:\nerror:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed"}
To check i am using the crocodoc API properly i wrote my own code
this is the error i am getting in my own code
::::OUTPUT::::
{"uuid": "300dc39d-e82b-4d92-a701-b1f376600b96"}
checking status of : {"uuid": "300dc39d-e82b-4d92-a701-b1f376600b96"}
Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Curl error no: 60
out put for docurlGet is :
bool(false) status is :
Viewing the Document : {"uuid": "300dc39d-e82b-4d92-a701-b1f376600b96"}
this is my code (i am sorry if i am not suppose to paste the whole code but could not understand the whole issue so i am just giving whole code )
<?php
$myToken='gSqV0PpEZhvJfLxQTcuMmoty';
$frameUrl='https://crocodoc.com/api/v2/document/upload';
$api_url = 'https://crocodoc.com/api/v2/';
//curl "https://crocodoc.com/api/v2/document/upload" --data "token=${API_TOKEN}&url=http://web.crocodoc.com/files/test-simple.pdf"
$param='token='.$myToken.'&url=http://web.crocodoc.com/files/test-simple.pdf';
//curl command
$ch = curl_init();
#curl_setopt($ch, CURLOPT_HEADER, 0);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'X-HTTP-Method-Override: POST'));
#curl_setopt($ch, CURLOPT_POST, 1);
#curl_setopt($ch, CURLOPT_URL, $frameUrl);
#curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$uuids = curl_exec($ch);
echo $uuids;
echo "<br>";
echo " checking status of : ", $uuids."<br>";
$status =getStatus($uuids,$api_url,$myToken);
//curl 'https://crocodoc.com/api/v2/document/status?token=F6JOdArWGKmDRlfPQcZna71x&uuids=d5ea0542-baaf-46a1-835c-685a86e70e14'
echo " status is : ", $status."<br>";
echo " Viewing the Document : ", $uuids."<br>";
function getStatus($uuids,$api_url,$myToken){
$isSingleUuid = is_string($uuids);
$obj = json_decode($uuids);
$my_uuid= $obj->uuid;
$url = $api_url.'document/status';
$token = $myToken;
$dataStr = '?token='.$token.'&uuids='.$my_uuid;
$output = my_doCurlGet($url, $dataStr);
var_dump($output);
return $output;
}
function my_doCurlGet($url, $dataStr) {
//var_dump($url.$dataStr);
// var_dump($dataStr);exit;
//
$ch1 = curl_init();
#curl_setopt($ch1, CURLOPT_HEADER, 0);
#curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json', 'X-HTTP-Method-Override: POST'));
#curl_setopt($ch1, CURLOPT_POST, 1);
#curl_setopt($ch1, CURLOPT_URL, $url.$dataStr);
#curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch1);
if(curl_exec($ch1) === false)
{
echo "<br>".'Curl error: ' . curl_error($ch1);
}
else
{
echo "<br>".'Operation completed without any errors'."<br>";
}
//var err_no=curl_errno()
if(curl_errno($ch1))
{
echo "<br>".'Curl error: ' . curl_error($ch1);
echo "<br>".'Curl error no: ' . curl_errno($ch1);
}
echo "<br>". "out put for docurlGet is : $output"."<br>";
return $output;
}
if (curl_errno($ch)) {
print 'An curl error has occurred.';
exit;
}
//var_dump($uuids);
curl_close($ch);
return $uuids;
?>
Thanks for all your help
Curl probably isn't properly configured with a CA bundle, or the configured CA bundle does not include Crocodoc's CA cert.
Check out this answer for more info.
Found the answer
First of all i did enable curl and SSL for beginning but it was not working
i even changed my Localhost name to myHost but it still did not work
so I made few changes in crocodoc.php file which i downloaded from crocodoc api library
CROCODOC PHP LIBRARY
I added this lines and IT WORKED :):):) Thank you all, hope this helps someone else as well.
if(($_SERVER['SERVER_NAME'] == 'myHost') && ($_SERVER['SERVER_ADDR'] == '127.0.0.1') && ($_SERVER['HTTP_HOST'] == 'myHost')) {
$options[CURLOPT_SSL_VERIFYPEER]=false;
}