C
C#14mo ago
T3CH

❔ c# post request http body

hi! im trying to make a post request to the spotify api, and i cant figure out how to add a body to the request with the value "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret" my current code is
var request = WebRequest.Create("https://accounts.spotify.com/api/token");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var request = WebRequest.Create("https://accounts.spotify.com/api/token");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
10 Replies
Buddy
Buddy14mo ago
Use HttpClient, it's more up-to date (faster) and a lot easier.
Angius
Angius14mo ago
var content = new FormUrlEncodedContent(...);
await _client.PostAsync(url, content);
var content = new FormUrlEncodedContent(...);
await _client.PostAsync(url, content);
That's the gist of it with HttpClient
T3CH
T3CHOP14mo ago
im pretty new to c# whats await
Angius
Angius14mo ago
$async
MODiX
MODiX14mo ago
For an introduction to async and await in C#, https://blog.stephencleary.com/2012/02/async-and-await.html
Async and Await
Most people have already heard about the new “async” and “await” functionality coming in Visual Studio 11. This is Yet Another Introductory Post.
T3CH
T3CHOP14mo ago
i'll check that out
Angius
Angius14mo ago
All you need to remember, though, is 1. If a method is async, you need to await it 2. If a method does any awaiting it needs to be async 3. If a method is async it has to return Task or Task<T>
T3CH
T3CHOP14mo ago
im stuck where i would go from here
public static string requestToken()
{
string token_url = "https://accounts.spotify.com/api/token";
Client.PostAsync(token_url, );
}
public static string requestToken()
{
string token_url = "https://accounts.spotify.com/api/token";
Client.PostAsync(token_url, );
}
how do i input the content type and the http body
MODiX
MODiX14mo ago
Angius
var content = new FormUrlEncodedContent(...);
await _client.PostAsync(url, content);
var content = new FormUrlEncodedContent(...);
await _client.PostAsync(url, content);
Quoted by
<@85903769203642368> from #c# post request http body (click here)
React with ❌ to remove this embed.
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server