0

I'm using a REST Service library with C# called RestSharp. I've been trying for too long to send information to a emailforge(newsletter service) server but i couldn't make it so far. I can post, put and get basic information, but i can't do the same with fields or lists within the Json sent.

RestClient client = new RestClient();
client.BaseUrl = "https://s3s.fr/api/rest/1/";
client.Authenticator = new HttpBasicAuthenticator(splioUser, splioPass);
var request = new RestRequest("{item}", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddUrlSegment("item", "contact");
request.AddParameter("email", "test@test.com.br");
request.AddParameter("firstname", "Jhonny");
request.AddParameter("lastname", "Test");
request.AddParameter("lang", "EN");

And that's the result:

{"email":"test@test.com.br","date":"2013-12-20 18:43:42","firstname":"Jhonny","lastname":"Test","lang":"EN","fields":[{"id":"0","name":"sexo","value":""},{"id":"1","name":"nascimento","value":""},{"id":"2","name":"cidade","value":""},{"id":"3","name":"estado","value":""},{"id":"4","name":"data altera\u00e7\u00e3o do cadastro","value":""},{"id":"5","name":"data \u00faltima compra","value":""},{"id":"6","name":"data primeira compra","value":""},{"id":"7","name":"data de criacao do cadastro","value":""},{"id":"8","name":"data de ultimo login no site","value":""}],"lists":[]}

It seems that i can't include any values between "lists" and "fields" values.

There's a manual orienting how to do it using PHP:

$universe = 'testapi'; 
$pass = 'MySecretAPIKEY'; 
$resource = ‘contact‘ ; 
$email = ‘old@s3s.fr’ $service_url = "https://$universe:$pass@s3s.fr/api/rest/1/$resource/$email"; 
$curl = curl_init($service_url); 
$query = array ( 'email' => ‘newmail@s3s.fr', 'lang' => 'de', 'fields' => array ( array ( 'id' => '0', 'value' => 'bl10'), array ( 'name' => 'field2', 'value' => 'myothercustomfield_value'), ), 'lists' => array ( array ( 'name' => 'Test Api' ), array ( 'id' => '5', 'action' => 'unsubscribe' ), ), ) ; 
$qstring = json_encode($query); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS,$qstring);
curl_setopt($curl,CURLOPT_HTTPHEADER,array("Expect:")); 
$curl_response = curl_exec($curl);
curl_close($curl); 
var_dump($curl_response); 
var_dump(json_decode($curl_response, true));

Thanks in advance!

1 Answers1

0

Using RestSharp, you should use the built in json serialization. See this previous answer for an example

ADDED

Also, I find that using Fiddler or a similar tool is very useful for debugging. Fiddler requires some configuration to monitor traffic for some use cases,

Community
  • 1
  • 1
Gary Walker
  • 8,831
  • 3
  • 19
  • 41
  • I am adding parameters tho. I already tried doing: Parameter[] fields = new Parameter[3]; fields[0] = new Parameter() { Name = "sexo", Value = "M" }; fields[1] = new Parameter() { Name = "nascimento", Value = "11/12/2013" }; fields[2] = new Parameter() { Name = "cidade", Value = "santos" }; request.AddParameter("fields", fields); request.AddBody(fields); – user3123564 Dec 20 '13 at 18:24