import os
import requests
response = requests.post(
url="https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments",
headers={"Authorization": f"token {os.environ['GITHUB_TOKEN']}"},
json={"body": "This is a test comment"}
)
response.raise_for_status()
import os
import httpx
response = httpx.post(
url="https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments",
headers={"Authorization": f"token {os.environ['GITHUB_TOKEN']}"},
json={"body": "This is a test comment"}
)
response.raise_for_status()
import fetch from 'node-fetch';
const response = await fetch("https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments", {
method: 'POST',
headers: {
'Authorization': `token ${process.env.GITHUB_TOKEN}`
},
body: JSON.stringify({"body": "This is a test comment"})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
import axios from 'axios';
const response = await axios.post(
"https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments",
{"body": "This is a test comment"},
{
headers: {
'Authorization': `token ${process.env.GITHUB_TOKEN}`
}
}
);
package main
import (
"fmt"
"net/http"
"os"
"io"
)
func main() {
client := &http.Client{}
req, err := http.NewRequest("POST", "https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments", nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "token "+os.Getenv("GITHUB_TOKEN"))
req.Header.Set("Content-Type", "application/json")
body := map[string]string{"body": "This is a test comment"}
if err := req.ParseForm(); err != nil {
panic(err)
}
// For JSON, we need to set the body separately
req.Body = io.NopCloser(strings.NewReader(`{"body":"This is a test comment"}`))
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(fmt.Sprintf("HTTP error! status: %s", resp.Status))
}
}
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["body" => "This is a test comment"]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . getenv('GITHUB_TOKEN'),
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new Exception("HTTP error! status: " . $httpCode);
}
curl_close($ch);
$body = json_decode($response, true);
require 'net/http'
require 'json'
require 'uri'
uri = URI.parse("https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request['Authorization'] = "token #{ENV['GITHUB_TOKEN']}"
request['Content-Type'] = 'application/json'
request.body = { body: "This is a test comment" }.to_json
response = http.request(request)
unless response.is_a?(Net::HTTPSuccess)
raise "HTTP error! status: #{response.code}"
end
import Foundation
let url = URL(string: "https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("token \(ProcessInfo.processInfo.environment["GITHUB_TOKEN"] ?? "")", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: String] = ["body": "This is a test comment"]
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (data, response) = try await URLSession.shared.data(for: request)
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
throw URLError(.badServerResponse)
}
use reqwest;
use std::env;
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let client = reqwest::Client::new();
let response = client
.post("https://api.github.com/repos/${GITHUB_OWNER}/repo/issues/42/comments")
.header("Authorization", format!("token {}", env::var("GITHUB_TOKEN").unwrap()))
.json(&serde_json::json!({"body": "This is a test comment"}))
.send()
.await?;
if !response.status().is_success() {
panic!("HTTP error! status: {}", response.status());
}
let _body = response.json::<serde_json::Value>().await?;
Ok(())
}
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.github.com");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", Environment.GetEnvironmentVariable("GITHUB_TOKEN"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent("{\"body\":\"This is a test comment\"}", System.Text.Encoding.UTF8, "application/json");
var response = await client.PostAsync("/repos/${GITHUB_OWNER}/repo/issues/42/comments", content);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"HTTP error! status: {response.StatusCode}");
}
var body = await response.Content.ReadAsStringAsync();
}
}