How to enable multiple answers when creating a SurveyMonkey collector using the API? - api

I have an application that creates Survey Monkey collectors for a survey using the 'create_collector' API method. That works perfectly. The problem is that it always creates collectors with the 'Multiple Responses' set to false. I need collectors created through the API to have 'Multiple Responses' set to true. There doesn't seem to be any option in the API call to specify that.
How can I set 'Multiple Responses' to true using the 'create_collector' API?
Here is my code in c#:
class CollectorJSON
{
public string type { get; set; }
public string name { get; set; }
}
private string CreateCollectorURL = "https://api.surveymonkey.net/v2/collectors/create_collector?api_key={API_KEY_VALUE}";
public CollectorResponseJSONWrapper CreateCollector(string APIKey, string AccessToken, long SurveyId, string CollectorName)
{
_LastException = null;
CollectorResponseJSONWrapper collectorRv = new CollectorResponseJSONWrapper();
collectorRv.status = -1;
try
{
string url = CreateCollectorURL.Replace("{API_KEY_VALUE}", APIKey);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "bearer " + AccessToken);
CollectorJSON collector = new CollectorJSON()
{
type = "weblink",
name = CollectorName
};
CollectorJSONWrapper wrapper = new CollectorJSONWrapper()
{
survey_id = SurveyId.ToString(),
collector = collector
};
string JSONData = JsonConvert.SerializeObject(wrapper);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(JSONData);
request.ContentType = "application/json";
request.ContentLength = bytes.Length;
Stream rstream = request.GetRequestStream();
rstream.Write(bytes, 0, bytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseBody = string.Empty;
using (var reader = new StreamReader(response.GetResponseStream(), ASCIIEncoding.ASCII))
{
responseBody = reader.ReadToEnd();
}
collectorRv = JsonConvert.DeserializeObject<CollectorResponseJSONWrapper>(responseBody);
}
catch( Exception ex )
{
_LastException = ex;
}
return collectorRv;
}
Thanks,

This was not supported in API v2 but can now be done using API v3, https://developer.surveymonkey.com/api/v3/#surveys-id-collectors
By setting the field allow_multiple_responses when doing a POST to /surveys/{id}/collectors to create a new collector.

Related

How to solve this circular dependency problem?

I have two interfaces for components that each requires functionality from the other one. One that generates Oauth tokens, and another one that gets secrets from a secret provider (Azure Key Vault).
The problem is that the Token Provider needs to obtain a secret value (a password) to make its HTTP call, and the Secret Provider class needs to get a Token in order to call Azure. Chicken and Egg problem.
From the other questions I've read, one suggestion is to create a third class/interface on which the original 2 depend, but I'm not sure how that would work here.
Any help and suggestions would be appreciated. Code for all relevant classes/interfaces is shown below.
public interface ISecretProvider
{
string GetSecret(string secretName);
}
public interface ITokenProvider
{
string GetKeyVaultToken();
}
public class OktaTokenProvider : ITokenProvider
{
ISecretProvider _secretProvider;
public string GetKeyVaultToken()
{
var tokenUrl = ConfigurationManager.AppSettings["KeyVault.Token.Url"];
var clientId = ConfigurationManager.AppSettings["KeyVault.Token.ClientId"];
var clientSecret = _secretProvider.GetSecret("ClientSecret");
var scope = ConfigurationManager.AppSettings["KeyVault.Scope"];
var token = GetToken(tokenUrl, clientId, clientSecret, scope);
return token;
}
private string GetToken(string tokenUrl, string clientId, string clientSecret, string scope)
{
var clientCredentials = $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"))}";
string responseFromServer = string.Empty;
bool success = false;
int retryCount = 0;
while (!success)
{
try
{
var tokenWebRequest = (HttpWebRequest)WebRequest.Create(tokenUrl);
tokenWebRequest.Method = "POST";
tokenWebRequest.Headers.Add($"Authorization:{clientCredentials}");
tokenWebRequest.Headers.Add("Cache-control:no-cache");
tokenWebRequest.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(tokenWebRequest.GetRequestStream()))
{
streamWriter.Write($"grant_type=client_credentials&scope={scope}");
streamWriter.Flush();
streamWriter.Close();
}
using (WebResponse response = tokenWebRequest.GetResponse())
{
using (var dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseFromServer = reader.ReadToEnd();
reader.Close();
}
dataStream.Close();
}
response.Close();
response.Dispose();
}
success = true;
}
catch (Exception)
{
if (retryCount > 3)
{
throw;
}
else
{
retryCount++;
}
}
}
JToken token = JObject.Parse(responseFromServer);
var accessToken = $"Bearer {token.SelectToken("access_token").ToString()}";
return accessToken;
}
}
public class KeyVaultSecretProvider : ISecretProvider
{
ITokenProvider _tokenProvider;
public KeyVaultSecretProvider(ITokenProvider tokenProvider)
{
_tokenProvider = tokenProvider;
}
public string GetSecret(string secretName)
{
var KeyVaultUrl = ConfigurationManager.AppSettings[Constants.KEYVAULT_ENDPOINT];
var subscriptionKey = ConfigurationManager.AppSettings[Constants.KEYVAULT_SUBSCRIPTION_KEY];
string responseFromServer = "";
var requestedSecretUrl = $"{KeyVaultUrl}{secretName}";
var secretWebRequest = (HttpWebRequest)WebRequest.Create(requestedSecretUrl);
var accessToken = _tokenProvider.GetKeyVaultToken();
secretWebRequest.Method = "GET";
secretWebRequest.Headers.Add("authorization:" + accessToken);
secretWebRequest.Headers.Add("cache-control:no-cache");
secretWebRequest.Headers.Add("Ocp-Apim-Subscription-Key:" + subscriptionKey);
using (WebResponse response = secretWebRequest.GetResponse())
{
using (var dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseFromServer = reader.ReadToEnd();
reader.Close();
}
dataStream.Close();
}
response.Close();
response.Dispose();
}
JToken secret = JObject.Parse(responseFromServer);
var secretValue = secret.SelectToken("Secret").ToString();
return secretValue;
}
}
Have a single class implement both interfaces. The two responsibilities are inter-dependent, so put them together in one class. There is nothing wrong with this.

Post a string to restful webservice in C#

I am provided a webservice url similar to:
http://ericdev35:7280/persons/persons/
and a username and password.
I want to make a post call on this web service from WPF application.
The data to be sent to the service is the first name and last name of a person in the format:
"fname=Abc&lname=Xyz"
How can I make a call for this in C#?
Here is the code that I have tried:
HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create("http://ericdev35:7280/persons/persons/");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Credentials = new NetworkCredential(username, password);
string data = "fname=Abc&lname=Xyz";
StreamWriter writer = new StreamWriter(httpWebRequest.GetRequestStream());
writer.Write(data);
writer.Close();
This does not give me any error but I cannot see the data that I have posted. Is there anything that needs to be corrected?
Is the Content Type correct?
This Method posts json.
After that it gets the response and deserialize the Json Object.
private static string PostJson<T1>(string p_url, string p_json, string p_method, out T1 p_target)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(p_url);
httpWebRequest.UseDefaultCredentials = true;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = p_method;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(p_json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse httpResponse;
try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (WebException ex)
{
httpResponse = ex.Response as HttpWebResponse;
}
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var a_result = streamReader.ReadToEnd();
//If you dont need a Json object delete anything behind here
try
{
p_target = JsonConvert.DeserializeObject<T1>(a_result);
}
catch { p_target = default(T1); }
return a_result;
}
}

passing object and stream to rest wcf

Here, when I am trying to pass both object and stream to wcf operation. I am getting "bad request 400" exception. If I pass only stream it is working fine with no issues and I am able to get output as stream. Any suggestions are greatly appreciated.
Client side code:
testclass tcls = new testclass();
tcls.name = "myclass";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(#"http://localhost:225141/RestService.svc/getobject/tc/{0}",tcls));
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("bda11d91-7ade-4da1-855d-24adfe39d154"));
req.Headers.Add("Authorization", "Basic " + svcCredentials);
req.MaximumResponseHeadersLength = 2147483647;
req.Method = "POST";
req.ContentType = "application/octet-stream";
FileStream fs = new FileStream("file1.txt", FileMode.Open, FileAccess.Read);
MemoryStream ms = new MemoryStream();
fs.CopyTo(ms);
ms.Position = 0;
byte[] dd = ms.ToArray();
Stream strw = req.GetRequestStream();
strw.Write(dd.ToArray(), 0, dd.Length);
strw.Close();
// here i am getting "bad request error"
using (WebResponse svcResponse = (HttpWebResponse)req.GetResponse())
{
MemoryStream msm = new MemoryStream();
svcResponse.GetResponseStream().CopyTo(msm);
msm.Position = 0;
byte[] data = msm.ToArray();
}
...
Service side code:
//IRestService.cs
[ServiceContract]
public interface IRestService
{
[OperationContract]
[WebInvoke(UriTemplate="getobject/tc/{tc}",Method="POST",
BodyStyle=WebMessageBodyStyle.Wrapped,ResponseFormat=WebMessageFormat.Json)]
Stream getobjectl(testclass obj,Stream tc);
}
[DataContract]
[KnownType(typeof(testclass))]
public class testclass
{
[DataMember]
public string name { get; set; }
}
//RestService.cs
public Stream getobject(testclass tc, Stream st)
{
//code;
}

2 exceptions when trying to make async HttpWebRequest

I am writing an MVC Web API the make async HttpWebRequest calls. I am getting 2 different exceptions. Below is the method I am using.
The 1st exception is: "This stream does not support seek operations." and it is happening on the responseStream.
The 2nd exception is: "timeouts are not supported on this stream" and that is happening on the MemoryStream content.
What am I doing wrong? I have been Googling but not really finding any solution.
Thanks,
Rhonda
private async Task GetHtmlContentAsync(string requestUri, string userAgent, string referrer, bool keepAlive, TimeSpan timeout, bool forceTimeoutWhileReading, string proxy, string requestMethod, string type)
{
//string to hold Response
string output = null;
//create request object
var request = (HttpWebRequest)WebRequest.Create(requestUri);
var content = new MemoryStream();
request.Method = requestMethod;
request.KeepAlive = keepAlive;
request.Headers.Set("Pragma", "no-cache");
request.Timeout = (Int32)timeout.TotalMilliseconds;
request.ReadWriteTimeout = (Int32)timeout.TotalMilliseconds;
request.Referer = referrer;
request.Proxy = new WebProxy(proxy);
request.UserAgent = userAgent;
try
{
using (WebResponse response = await request.GetResponseAsync().ConfigureAwait(false))
{
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
await responseStream.CopyToAsync(content);
}
}
var sr = new StreamReader(content);
output = sr.ReadToEnd();
sr.Close();
}
}
catch (Exception ex)
{
output = string.Empty;
var message = ("The API caused an exception in the " + type + ".\r\n " + requestUri + "\r\n" + ex);
Logger.Write(message);
}
return output;
}
I fixed the issue by adding
content.Position = 0
before new StreamReader line. Now I just need to get it work with GZip compression.
Rhonda

How to pass array of byte to WCF service

I want to pass array of byte to WCF service from my windows phone app.The url is like below
"http:/serviceurl/Service1.svc/saverecord3?firstarray="
How can i do this?
Here a part of my code, which I am using to send post data to my server
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http:/serviceurl/Service1.svc/saverecord3");
req.Method = "POST";
req.ContentType = "text/xml; charset=utf-8"; //if your data is different, use a content type accordingly
string postData = #"firstarray=thedatatosend";
int length = arrayData.Length;
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(arrayData);
req.Headers[HttpRequestHeader.ContentLength] = byteArray.Length.ToString();
req.BeginGetRequestStream(ar =>
{
using (var requestStream = req.EndGetRequestStream(ar))
{
// Write the body of your request here
requestStream.Write(byteArray, 0, length);
}
req.BeginGetResponse(a =>
{
try
{
var response = req.EndGetResponse(a);
var responseStream = response.GetResponseStream();
using (var streamRead = new StreamReader(responseStream))
{
// Get the response message here
string responseString = streamRead.ReadToEnd();
//Do whatever you want here, with the response
}
}
catch (Exception e)
{
}
}, null);
}, null);