✅ Help with http request

Hey guys, i'm having a wierd issue, i made a trial request to my mysql service provider via postman and it works fine, but when i try to do it via c# code, it returns with a 401, what's wierd is i used same auth ticket for both:
curl --location 'https://spending-juanthehuman.turso.io/v2/pipeline' \
--header 'Content-Type: application/json' \
--header 'Authorization: ••••••' \
--data '{"requests": [{"type": "execute","stmt": {"sql": "SELECT * FROM records"}},{"type": "close"}]}'
curl --location 'https://spending-juanthehuman.turso.io/v2/pipeline' \
--header 'Content-Type: application/json' \
--header 'Authorization: ••••••' \
--data '{"requests": [{"type": "execute","stmt": {"sql": "SELECT * FROM records"}},{"type": "close"}]}'
8 Replies
Mąż Zuzanny Harmider Szczęście
example usage:
public static async Task<List<Record>> GetRecordsAsync()
{
Debug.WriteLine(UserSecretsService.GetSecret("Turso:Token"));
Record[] recordsArr = await ApiService.Fetch<Record[]>(ConnectionString,new FetchOptions { Authorization=UserSecretsService.GetSecret("Turso:Token"),Method="POST"});
if (recordsArr == null)
{
return new List<Record>();
}
return recordsArr.ToList();
}
public static async Task<List<Record>> GetRecordsAsync()
{
Debug.WriteLine(UserSecretsService.GetSecret("Turso:Token"));
Record[] recordsArr = await ApiService.Fetch<Record[]>(ConnectionString,new FetchOptions { Authorization=UserSecretsService.GetSecret("Turso:Token"),Method="POST"});
if (recordsArr == null)
{
return new List<Record>();
}
return recordsArr.ToList();
}
and i checked, the UserSecretsService returns the token, not null or empty string
Pobiega
Pobiega2w ago
if (options.Body != null)
{
request.Content = new StringContent(JsonSerializer.Serialize(options.Method), Encoding.UTF8, "application/json");
}
if (options.Body != null)
{
request.Content = new StringContent(JsonSerializer.Serialize(options.Method), Encoding.UTF8, "application/json");
}
seems wrong to me. You are serializing the HTTP method and putting that as your... content? you dont include your actual request payload anywhere
Angius
Angius7d ago
Your code seems a fair bit overcomplicated, if you ask me.
var client = new HttpClient();

client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = "...";

var res = await client.PostAsJsonAsync("https://spending-juanthehuman.turso.io/v2/pipeline", data);
res.EnsureSuccessStatusCode();

var receivedData = await res.ReadFromJsonAsync<ResponseData>();
var client = new HttpClient();

client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = "...";

var res = await client.PostAsJsonAsync("https://spending-juanthehuman.turso.io/v2/pipeline", data);
res.EnsureSuccessStatusCode();

var receivedData = await res.ReadFromJsonAsync<ResponseData>();
I'm assuming you want to POST to that endpoint, seeing how GET doesn't support body (a very useful website btw) Though it uses StringContent, whereas it would probably be best to just use JsonContent so you don't need to set the content-type manually
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Mąż Zuzanny Harmider Szczęście
its not a web app, all i need the api for is database storage, coz i plan on making both a mobile and windows app, so that i can add records via both
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX6d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?