Yes I m writing a programme in C

Yes, I'm writing a programme in C#.
4 Replies
Chaika
Chaika16mo ago
Sure, something like this:
// Appending 'worker.js' field
string workerJsContent = $@"export default {{
async fetch(request, env, ctx) {{
return new Response('Hello World!');
}},
}};".ReplaceLineEndings(" "); // I had issues with the line endings without this..


var metadata = System.Text.Json.JsonSerializer.Serialize(new
{
compatibility_date = "2023-07-26",
usage_model = "bundled",
main_module = "worker.js"
// You can configure tail_consumers, placementmode, and logpush here as well.
});


var formData = new MultipartFormDataContent();

var workerScriptContent = new StringContent(workerJsContent);
workerScriptContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/javascript+module");

formData.Add(workerScriptContent, "worker.js", "worker.js");


var metadataContent = new StringContent(metadata);
metadataContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
formData.Add(metadataContent, "metadata", "blob");


var newHttpClient = new HttpClient(); // or get from DI

var request = new HttpRequestMessage(HttpMethod.Put,
$"https://api.cloudflare.com/client/v4/accounts/{accountId}/workers/services/{scriptName}/environments/production");
request.Headers.Add("Authorization", $"Bearer {apiToken}");
request.Content = formData;
var response = await newHttpClient.SendAsync(request);
// Appending 'worker.js' field
string workerJsContent = $@"export default {{
async fetch(request, env, ctx) {{
return new Response('Hello World!');
}},
}};".ReplaceLineEndings(" "); // I had issues with the line endings without this..


var metadata = System.Text.Json.JsonSerializer.Serialize(new
{
compatibility_date = "2023-07-26",
usage_model = "bundled",
main_module = "worker.js"
// You can configure tail_consumers, placementmode, and logpush here as well.
});


var formData = new MultipartFormDataContent();

var workerScriptContent = new StringContent(workerJsContent);
workerScriptContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/javascript+module");

formData.Add(workerScriptContent, "worker.js", "worker.js");


var metadataContent = new StringContent(metadata);
metadataContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
formData.Add(metadataContent, "metadata", "blob");


var newHttpClient = new HttpClient(); // or get from DI

var request = new HttpRequestMessage(HttpMethod.Put,
$"https://api.cloudflare.com/client/v4/accounts/{accountId}/workers/services/{scriptName}/environments/production");
request.Headers.Add("Authorization", $"Bearer {apiToken}");
request.Content = formData;
var response = await newHttpClient.SendAsync(request);
Should be a decent start, I ripped this from a larger project so lmk if some of it needs more explanation. FormData makes it semi-easy to do. If you want something more advanced like bindings, the easiest way is to update a worker that has bindings in dash, inspect the api call (ctrl+shift+i) it makes The wrangler/workers-sdk is another good source to steal stuff from, to see how it does it. There's not really much good documentation on the various types as far as I know. I just use QuickType for the response info, can share those as well if useful
lokiwind
lokiwindOP16mo ago
Thank you very much. I will not create a very detailed worker. I just want to create a default worker That's enough for now. I think this code will work for me thank you. Chaika
Chaika
Chaika16mo ago
ah ok, I use it to update but if you do it on a worker that doesn't exist, it just creates it. If it's worth explaining as well, the environment path there is always production, they used to be useful for service environments but CF abandoned those
lokiwind
lokiwindOP16mo ago
Hmm got it. Thank you very much for this information and code
Want results from more Discord servers?
Add your server