Al - Hi, I'm trying to call this API via C# cod...

Hi, I'm trying to call this API via C# code. I tested it in Postman, and it only accepts x-www-form-urlencoded, throwing an error when I use JSON. My issue is that I need to pass an array of strings, and despite serializing the ids part as a string[], I’m still getting a 500 error from the server. Additionally, I found that the API only works with callosum/v1 included in the URL, which makes the documentation a bit unclear. Do you have an example of how to handle this in C#?
No description
Solution:
The userId that you gave is supposed to be userid, can you check once using that? Also I gave this curl to chatgpt
curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'type=PINBOARD_ANSWER_BOOK&ids=%5B%27fded567b-8047-4185-b702-cee652725ac7%27%2C%27c88c508c-5965-4c85-9cec-236c5ef57ac5%27%5D&userid=7e9b23ce-f954-426a-986a-d1aa8be52414' 'https://{tsurl}/callosum/v1/tspublic/v1/metadata/markunmarkfavoritefor'
curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'type=PINBOARD_ANSWER_BOOK&ids=%5B%27fded567b-8047-4185-b702-cee652725ac7%27%2C%27c88c508c-5965-4c85-9cec-236c5ef57ac5%27%5D&userid=7e9b23ce-f954-426a-986a-d1aa8be52414' 'https://{tsurl}/callosum/v1/tspublic/v1/metadata/markunmarkfavoritefor'
to code for C# this is what is produced : ```...
Jump to solution
18 Replies
shikharTS
shikharTS3mo ago
I might need to check this, but does ids=['guid1','guid2'] does not work? Can you check how the data is being sent in the request in C#? It should be language agnostic if the request being formed is correct Also both in the request URL and in the cURL url we have callosum/v1 included in URL, the resource URL is just to understand the endpoint (since every v1 endpoint will have callosum/v1 as its prefix we have not included it in the endpoint URL) rather than full URL, we have request url given there. Can you suggest how to improve the documentation here?
Al
AlOP3mo ago
no it does not work because in C#, to pass something as x-www-form-urlencoded, you need to use FormUrlEncodedContent which takes a KeyValuePair<string, string>. The parameter ids is setup to be an array of id. This is what makes it complicated. As far as the doc is concern, I guess you could just include callosum/v1 in everything for consistency. But that's just me. Anyway, it's a minor thing. I just want to be able to get the API to work.
Al
AlOP3mo ago
btw this is what my request content looks like. Still throws a 500 error from the server
No description
Al
AlOP3mo ago
here's the full method for your reference
No description
Al
AlOP3mo ago
No description
Al
AlOP3mo ago
here's the error I get
No description
shikharTS
shikharTS3mo ago
@pallav can you check this?
Al
AlOP3mo ago
Hello, I just wanted to kindly follow up on this.
shikharTS
shikharTS3mo ago
nvm let me check how to do this for C#
Al
AlOP3mo ago
yes please, thank you
Solution
shikharTS
shikharTS3mo ago
The userId that you gave is supposed to be userid, can you check once using that? Also I gave this curl to chatgpt
curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'type=PINBOARD_ANSWER_BOOK&ids=%5B%27fded567b-8047-4185-b702-cee652725ac7%27%2C%27c88c508c-5965-4c85-9cec-236c5ef57ac5%27%5D&userid=7e9b23ce-f954-426a-986a-d1aa8be52414' 'https://{tsurl}/callosum/v1/tspublic/v1/metadata/markunmarkfavoritefor'
curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'type=PINBOARD_ANSWER_BOOK&ids=%5B%27fded567b-8047-4185-b702-cee652725ac7%27%2C%27c88c508c-5965-4c85-9cec-236c5ef57ac5%27%5D&userid=7e9b23ce-f954-426a-986a-d1aa8be52414' 'https://{tsurl}/callosum/v1/tspublic/v1/metadata/markunmarkfavoritefor'
to code for C# this is what is produced :
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
static async Task Main()
{
using (HttpClient client = new HttpClient())
{
// Set headers
client.DefaultRequestHeaders.Add("Accept", "application/json");

// Prepare the form data
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("type", "PINBOARD_ANSWER_BOOK"),
new KeyValuePair<string, string>("ids", "['fded567b-8047-4185-b702-cee652725ac7','c88c508c-5965-4c85-9cec-236c5ef57ac5']"),
new KeyValuePair<string, string>("userid", "7e9b23ce-f954-426a-986a-d1aa8be52414")
});

// Make the POST request
HttpResponseMessage response = await client.PostAsync(
"https://{tsurl}/callosum/v1/tspublic/v1/metadata/markunmarkfavoritefor",
content
);

// Read response
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
static async Task Main()
{
using (HttpClient client = new HttpClient())
{
// Set headers
client.DefaultRequestHeaders.Add("Accept", "application/json");

// Prepare the form data
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("type", "PINBOARD_ANSWER_BOOK"),
new KeyValuePair<string, string>("ids", "['fded567b-8047-4185-b702-cee652725ac7','c88c508c-5965-4c85-9cec-236c5ef57ac5']"),
new KeyValuePair<string, string>("userid", "7e9b23ce-f954-426a-986a-d1aa8be52414")
});

// Make the POST request
HttpResponseMessage response = await client.PostAsync(
"https://{tsurl}/callosum/v1/tspublic/v1/metadata/markunmarkfavoritefor",
content
);

// Read response
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
shikharTS
shikharTS3mo ago
let me know if this helps
Al
AlOP3mo ago
so the ids parameter expects a string[], but what if I only pass one? it's not gonna treat it as array
shikharTS
shikharTS3mo ago
no it won't. Though just giving a single element in the array should work new KeyValuePair<string, string>("ids", "['fded567b-8047-4185-b702-cee652725ac7']"), should be fine
Al
AlOP3mo ago
tried it. it doesn't work. here's the error
No description
Al
AlOP3mo ago
I wish we can just get it to accept json, is that not possible?
Al
AlOP3mo ago
here's what the param looks like
No description
Al
AlOP3mo ago
actually, it works. Didn't notice that I had the I in userId as uppercase thanks for the help

Did you find this page helpful?