0

I would like to change the layer's style via REST. My problem is very similar to here and here.

Running the following via curl works:

curl.exe -v -u user:password -XPUT -H "Content-type: text/xml" 
-d "<layer><defaultStyle><name>poi</name></defaultStyle></layer>" 
http://localhost:8080/geoserver/rest/layers/workspace:layer

Though, I'm building on top of GeoServerWrapper in PHP provided by IBM to interact with the GeoServer REST API so my code is as follows:

private function runApi($apiPath, $method = 'GET', $data = '', $contentType = 'text/xml') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->serverUrl.'rest/'.$apiPath);
    curl_setopt($ch, CURLOPT_USERPWD, $this->username.":".$this->password); 
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    } else if ($method == 'DELETE' || $method == 'PUT') {
        // return $method;
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    }

    if ($data != '') {
        curl_setopt($ch, CURLOPT_HTTPHEADER, 
            array("Content-Type: $contentType",
            'Content-Length: '.strlen($data))
        );
    }

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $rslt = curl_exec($ch);
    $info = curl_getinfo($ch);

    if ($info['http_code'] == 401) {
        return 'Access denied. Check login credentials.';
    } else {
        return $rslt;
    }
}

public function addStyleToLayer($layerName, $workspaceName, $styleName) {
    return $this->runApi('layers/'.urlencode($workspaceName).':'.urlencode($layerName).'', 'PUT', '<layer><defaultStyle><name>'.htmlentities($styleName, ENT_COMPAT).'</name></defaultStyle><enabled>true</enabled></layer>');
}

Specifying 'PUT' didn't work so I'm back to the drawing boards.

EDIT, I'm sending these parameters to the GeoServer REST:

$.ajax({
               url: 'geoserverRestFinal.php',
               type: 'PUT',
               data: {
                'action': 'assignstyle',
                'username': $('#username').val(),
                'password': $('#password').val(),
                'workspace': $('#assignstyle_workspace').val(),
                'layer': $('#assignstyle_layer').val(),
                'stylename': $('#assignstyle_stylename').val()
               },
               success: function(ret) {
                $('#assignstyle_results').html(escape(ret).replace(/%(..)/g,"&#x$1;")); 
               }
            });

From the 'action' parameter:

if (isset($_REQUEST['action'])) {
include "GeoserverWrapper.php";
$geoserver = new GeoserverWrapper('http://server:8080/geoserver/', $_REQUEST['username'], $_REQUEST['password']);

switch ($_REQUEST['action']) {
    case 'assignstyle':
        print_r($geoserver->addStyleToLayer($_REQUEST['layer'], $_REQUEST['workspace'], $_REQUEST['stylename']));
        break;
}

return;}

EDIT 2, my jQuery Ajax call is sending my data as Form Data, not as Query String Parameters.

Current:

http://server/php/geoserverRestFinal.php

Form Data action:assignstyle username:user password:password workspace:geoportal layer:glocalidad stylename:point

Should be:

http://server/php/geoserverRestFinal.php?action=assignstyle&username=user&password=password&workspace=geoportal&layer=glocalidad&stylename=point

Looking into jQuery.params

  • can you show us what is actually sent to the server by your code so we can compare it to the curl example? – Ian Turton Dec 4 '14 at 9:14
  • edit 2 has what is being sent to the server – Zach Dec 4 '14 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.