0
Hello, I’m having trouble converting a PHP API to be used in C#, I can connect to the API but I can’t send data, someone could help me?
Follows API in PHP:
$process = curl_init("URL API");
curl_setopt($process, CURLOPT_USERPWD, "USUARIO:SENHA");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_POST, true);
curl_setopt($process, CURLOPT_POSTFIELDS, array(
"establishment" => $establishment,
"username" => $usuario,
"password" => $senha,
"name" => $nome,
"email" => $email,
"cpf" => $cpf,
"login_expiration" => $expirarLogin,
"profile_id" => $perfil
));
if(json_decode(curl_exec($process)) === false){
return FALSE;
}else{
return TRUE;
}
Follow what I’ve been able to do so far using C#:
using RestSharp;
var cookie = new CookieContainer();
var client = new RestClient("URL API") {
Authenticator = new HttpBasicAuthenticator("USUARIO", "SENHA"),
CookieContainer = cookie
};
var request = new RestRequest(Method.POST);
//var process = new RestClient(Method.POST);
//request.AddParameter(""", "projectID=729-11230-1", ParameterType.RequestBody);
//request.AddParameter("application/x-www-form-urlencoded", "name=Example backlog item 1", ParameterType.RequestBody);
//request.AddParameter("application/x-www-form-urlencoded", "establishment=sesc_new", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "username=user", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "password=senha", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "name=Teste API Sharp", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "email=email", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "cpf=cpf", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "login_expiration=2018-07-20 23:59:59", ParameterType.GetOrPost);
//request.AddParameter("application/x-www-form-urlencoded", "profile_id=4080", ParameterType.GetOrPost);
IRestResponse response = client.Execute(request);
MessageBox.Show(response.Content);
I can make the connection, but now I don’t know how to send the information to the API from C#.
By the content you are printing, c# is already communicating with the API. And returning the data correctly. Now you just need to treat the return.
– Eric Wu
Well, it’s just that I wanted to do the opposite, rather than return something from there, I wanted to send something there. This is what I’m not getting, because in the API is sending and no return.
– Danillo Dars