1

I would like assign a user to a group with API REST.

But it doesn't work.

I use POST /groups/:id/users.:format syntax (see Rest Groups)

User with this id exists in redmine and group too.

In redmine log I can see:

Processing by GroupsController#add_users as XML
Parameters: {"group"=>{"user_id"=>"34"},   "key"=>"81aa228c55ac5cfe4264a566ef67ac27702da8eb", "id"=>"5"}
Current user: admin (id=1)
Rendering common/error_messages.api.rsb
Rendered common/error_messages.api.rsb (0.1ms)
Completed 422 Unprocessable Entity in 4ms (Views: 0.4ms |  ActiveRecord: 1.3ms)

And in API's response:

Code Error :422
Message : User is invalid
In request body : id of user

I use ActiveResouce for REST API.

$method = 'users'
$options = array('user_id' => user's id to add)

/**
 * Posts to a specified custom method on the current object via:
 *
 *     POST /collection/id/method.xml
 */
function post ($method, $options = array (), $start_tag = false) {
    $req = $this->site . $this->element_name_plural;
    if ($this->_data['id']) {
        $req .= '/' . $this->_data['id'];
    }
    $req .= '/' . $method . '.xml';
    return $this->_send_and_receive ($req, 'POST', $options, $start_tag);
}

And this function for send request and parse the response :

/**
     * Build the request, call _fetch() and parse the results.
     */
    function _send_and_receive ($url, $method, $data = array (), $start_tag = false) {
        $params = '';
        $el = $start_tag ? $start_tag : $this->element_name; // Singular this time
        if ($this->request_format == 'url') {
            foreach ($data as $k => $v) {
                if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
                    $params .= '&' . $el . '[' . str_replace ('-', '_', $k) . ']=' . rawurlencode ($v);
                }
            }
            $params = substr ($params, 1);
        } elseif ($this->request_format == 'xml') {
            $params = '<?xml version="1.0" encoding="UTF-8"?><' . $el . ">\n";
            foreach ($data as $k => $v) {
                if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
                    $params .= $this->_build_xml ($k, $v);
                }
            }
            $params .= '</' . $el . '>';
        }

        if ($this->extra_params !== false) {
            if(strpos($url, '?'))
            {
                $url = $url .'&'.$this->extra_params;
            }
            else
            {
                $url = $url .'?'.$this->extra_params;
            }

        }

        $this->request_body = $params;
        $this->request_uri = $url;
        $this->request_method = $method;

        $res = $this->_fetch ($url, $method, $params);

        if ($res === false) {
            return $this;
        }

        // Keep splitting off any top headers until we get to the (XML) body:
        while (strpos($res, "HTTP/") === 0) {
            list ($headers, $res) = explode ("\r\n\r\n", $res, 2);
            $this->response_headers = $headers;
            $this->response_body = $res;
            if (preg_match ('/HTTP\/[0-9]\.[0-9] ([0-9]+)/', $headers, $regs)) {
                $this->response_code = $regs[1];
            } else {
                $this->response_code = false;
            }

            if (! $res) {
                return $this;
            } elseif ($res == ' ') {
                $this->error = 'Empty reply';
                return $this;
            }
        }

        // parse XML response
        $xml = new SimpleXMLElement ($res);

        // normalize xml element name in case rails ressource contains an underscore
        if (str_replace ('-', '_', $xml->getName ()) == $this->element_name_plural) {
            // multiple
            $res = array ();
            $cls = get_class ($this);
            foreach ($xml->children () as $child) {
                $obj = new $cls;
                foreach ((array) $child as $k => $v) {
                    $k = str_replace ('-', '_', $k);
                    if (isset ($v['nil']) && $v['nil'] == 'true') {
                        continue;
                    } else {
                        $obj->_data[$k] = $v;
                    }
                }
                $res[] = $obj;
            }
            return $res;
        } elseif ($xml->getName () == 'errors') {
            // parse error message
            $this->error = $xml->error;
            $this->errno = $this->response_code;
            return false;
        }

        foreach ((array) $xml as $k => $v) {
            $k = str_replace ('-', '_', $k);
            if (isset ($v['nil']) && $v['nil'] == 'true') {
                continue;
            } else {
                $this->_data[$k] = $v;
            }
        }

        return $this;
    }

Thank you

1
  • you ought to add the code that you use to send the request as that is likely where the issue is Jun 26, 2019 at 7:20

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.