You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While people using the HTTPClient in a `using` statement or creating a new one within the executed function may be a common occurence, it is heavily discouraged to do so. [Read More](https://extensions.streamer.bot/t/httpclient-and-you/1369)
68
+
::
69
69
70
70
2. Setup the sending of your payload
71
71
72
72
- We are going to send a `%userName%` and `%userId%` pair (from common triggers) as `PUT` request to our fictional logging website.
73
73
74
74
```cs [Send PUT payload in Execute method]
75
-
publicboolExecute() {
76
-
// Get the arguments - or abort if it does not exist
- To use different request types, simply use the methods `GetAsync()`, `PutAsync()`, `PostAsync()` or `DeleteAsync()`.
103
103
104
104
3. The PATCH request
105
105
106
106
- Specifically the built-in handling for the `PATCH` request type may not be available in the .net versions used by StreamerBot. In this case, you need a workaround.
107
-
108
-
```cs [PATCH workaround]
109
-
// ...
110
-
111
-
// Finally, send the request
112
-
var request = new HttpRequestMessage(newHttpMethod("PATCH"), "https://my-logging-server.com"){
- If you need to additionally include specific headers - like submitting an authorization token for the Twitch API or Discord API - make sure to clear and set the headers on each call you make.
123
-
1. You can also send individual headers with each request itself, but that requires you to always use the `SendAsync` method and building your own `HttpRequestMessage`.
124
-
125
-
- If your code only does a static request whose headers never change, resetting the headers is **not strictly necessary**. But it's good practice to not forget later when you need it.
126
-
127
-
```cs [Header management]
128
-
public bool Execute() {
129
-
// Making a call to the Twitch API to get all currently live streams
1. You can also send individual headers with each request itself, but that requires you to always use the `SendAsync` method and building your own `HttpRequestMessage`.
124
+
125
+
- If your code only does a static request whose headers never change, resetting the headers is **not strictly necessary**. But it's good practice to not forget later when you need it.
126
+
127
+
```cs [Header management]
128
+
publicboolExecute() {
129
+
// Making a call to the Twitch API to get all currently live streams
// Usually, data is JSON. So you would decode it now.
174
+
JObjectparsed=JObject.Parse(content);
175
+
176
+
// And then do with the data whatever you need.
177
+
// ...
178
+
179
+
returntrue;
180
+
} catch (Exceptione) {
181
+
// Something went wrong
182
+
CPH.LogError(e.Message);
183
+
returnfalse;
184
+
}
185
+
}
186
+
```
187
187
188
188
## Tips & Tricks
189
189
190
190
- Wrapping sensitive parts of code, especially when using web requests, into `try``catch` blocks is good practice and prevents potential unexpected bigger issues you do not anticipate.
0 commit comments