Mattermost, Inc.

Can’t add user to mattermost via API v4 (Post request not responding)

Hi,
Hello,
The version of the application we use is 5.19.1. When I try to add users to this application with a “POST” request, the application does not respond. I can get user information with “get” request. When I try to add the same user with the “Post” request, the error also returns, but the system does not respond when adding a new user.
I would be glad if you could help.

Hey @uzunsoy,
could you post the query you sent to the server here, of cours with sensible info like the auth token and eventual names removed? Please enclose the code in triple backticks like so:

```
this is where your code goes
```

Also can you post the error that is being returned?

Thanks!

Hi @sven.huester

Since the code I use does not return a response, the page that the code runs on also returns the error “The page you are looking for cannot be reached”. Unfortunately, the result does not change when I make the same CURL connection from the root console.
I using code below;

Mattermost.php

public function registerUser()
{
if ($this->email == “” || $this->username == “” || $this->password == “” || $this->token == “”) {
die(“Email, username, password ve token null!”);
}
$cookieSettingUrl = $this->map_apiUrl;
$cookie = CurlWrapper::SendRequest($cookieSettingUrl); // Info: Cookie

    /**
     * 
     * <pre>
     * <a href ="https://api.mattermost.com/#tag/users" >Link</a>
     *
     * </pre>
     *
     * @var array $data
     */
    $data = array(
        "email" => $this->email,
        "username" => $this->username,
        "password" => $this->password,
        "allow_marketing" => true,
        "auth_service" => "",
        "auth_data" => "",
        "locale" => "en",
        "props" => array(),
        "RequireEmailVerification" => false,
        "notify_props" => array(
            "email" => true,
            "push" => "all",
            "desktop" => "all",
            "desktop_sound" => "true",
            "channel" => "@all"
        )
    );

    $postData = json_encode($data);

    $serverUrl = $this->map_apiUrl . $this->path_users;
    $headers = array(
        'Accept: application/json',
        'Content-Type: application/json',
        'Authorization: Bearer ' . $this->token
    );

    $resultsHTML = CurlWrapper::SendRequest($serverUrl, $cookieSettingUrl, "POST", $postData, $headers);
    var_dump($resultsHTML);
    $resultsJson = json_decode($resultsHTML);
    return $resultsJson;
}

File: CurlWrapper_static.php

class CurlWrapper {

private static $useragents = array(            
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; WOW64; Trident/4.0; SLCC1)",
    "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14"
);

private static $cookiesFile = "curlCookies.txt";

private static function getUserAgent() {
	$rand = rand(0, count(self::$useragents) - 1);
	
	return self::$useragents[$rand];
}


public static function SendRequest($url, $ref = "", $type = "GET", $postData = "", $headers = "", $proxy = "") {
    //$useragent = self::getUserAgent();
    
    $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_TIMEOUT,0);
	//curl_setopt($ch, CURLOPT_USERAGENT, self::getUserAgent());
	
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	//curl_setopt($ch, CURLOPT_AUTOREFERER, true);
	curl_setopt($ch, CURLOPT_VERBOSE, true);
	curl_setopt($ch, CURLOPT_STDERR, fopen('php://stderr', 'w'));
	curl_setopt($ch, CURLOPT_COOKIEJAR, realpath(self::$cookiesFile)); 
	curl_setopt($ch, CURLOPT_COOKIEFILE, realpath(self::$cookiesFile));
	
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	
    //options
    if ($ref != "") {
        curl_setopt($ch, CURLOPT_REFERER, $ref);
    }
	
	if ($proxy != "") {
		curl_setopt($ch, CURLOPT_PROXY, $proxy);
	}
	
	if ($type == "POST"){
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
	}else if($type == "PUT"){		    
	    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");		    
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);		    		    
	}else if($type == "DELETE"){
	    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
	}
	

	if ($headers != ""){
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	}

    $result = curl_exec($ch);
	curl_close($ch);

	return $result;
}

}

Does this mean if you run the same request via CURL in a terminal you also get an error?

In that case it’s very likely that the request is incorrect altogether or you’re having issues with your environment.

Can you also post the curl command you’re running? Might be easier to debug that first instead of the PHP implementation :slight_smile: