import os
import requests
response = requests.post(
url="https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls",
headers={
"Authorization": f"token {os.environ['GITHUB_TOKEN']}",
"Accept": "application/vnd.github.v3+json"
},
json={"title": "Fix bug", "head": "feature-branch", "base": "main"}
)
response.raise_for_status()
print(response.json())
import os
import httpx
with httpx.Client() as client:
response = client.post(
url="https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls",
headers={
"Authorization": f"token {os.environ['GITHUB_TOKEN']}",
"Accept": "application/vnd.github.v3+json"
},
json={"title": "Fix bug", "head": "feature-branch", "base": "main"}
)
response.raise_for_status()
print(response.json())
import fetch from 'node-fetch';
import * as process from 'process';
(async () => {
const response = await fetch("https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls", {
method: 'POST',
headers: {
'Authorization': `token ${process.env.GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({"title": "Fix bug", "head": "feature-branch", "base": "main"})
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
console.log(await response.json());
})();
import axios from 'axios';
import * as process from 'process';
(async () => {
const response = await axios.post(
"https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls",
{"title": "Fix bug", "head": "feature-branch", "base": "main"},
{
headers: {
'Authorization': `token ${process.env.GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
console.log(response.data);
})();
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
func main() {
client := &http.Client{}
url := fmt.Sprintf("https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls")
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "token "+os.Getenv("GITHUB_TOKEN"))
req.Header.Set("Accept", "application/vnd.github.v3+json")
body := map[string]interface{}{
"title": "Fix bug",
"head": "feature-branch",
"base": "main",
}
bodyBytes, _ := json.Marshal(body)
req.Body = http.NoBody
req.Body = http.NoBody
req.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(bodyBytes)), nil
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalf("HTTP error! status: %s", resp.Status)
}
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
fmt.Println(result)
}
<?php
$ch = curl_init();
$url = sprintf("https://api.github.com/repos/%s/%s/pulls", getenv('GITHUB_OWNER'), getenv('GITHUB_REPO'));
$payload = json_encode([
'title' => 'Fix bug',
'head' => 'feature-branch',
'base' => 'main'
]);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . getenv('GITHUB_TOKEN'),
'Accept: application/vnd.github.v3+json',
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die('Error:' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200 && $httpCode !== 201) {
die("HTTP error! status: $httpCode");
}
$decoded = json_decode($response, true);
print_r($decoded);
curl_close($ch);
require 'net/http'
require 'json'
require 'uri'
uri = URI.parse("https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls")
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['Accept'] = 'application/vnd.github.v3+json'
request.body = {
title: 'Fix bug',
head: 'feature-branch',
base: 'main'
}.to_json
response = http.request(request)
unless response.is_a?(Net::HTTPSuccess)
raise "HTTP error! status: #{response.code}"
end
puts JSON.parse(response.body)
import Foundation
let url = URL(string: "https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/pulls")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("token \(ProcessInfo.processInfo.environment["GITHUB_TOKEN"] ?? "")", forHTTPHeaderField: "Authorization")
request.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
let body: [String: Any] = [
"title": "Fix bug",
"head": "feature-branch",
"base": "main"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
fatalError("Error: \(error)")
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
fatalError("HTTP error! status: \((response as? HTTPURLResponse)?.statusCode ?? 0)")
}
if let data = data, let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
print(json)
}
}
task.resume()
RunLoop.main.run()
use std::collections::HashMap;
use std::env;
use reqwest::blocking::Client;
use reqwest::header;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let url = format!(
"https://api.github.com/repos/{}/{}/pulls",
env::var("GITHUB_OWNER")?,
env::var("GITHUB_REPO")?
);
let mut headers = header::HeaderMap::new();
headers.insert(
"Authorization",
format!("token {}", env::var("GITHUB_TOKEN")?).parse()?,
);
headers.insert(
"Accept",
"application/vnd.github.v3+json".parse()?,
);
let body: HashMap<&str, &str> = HashMap::from([
("title", "Fix bug"),
("head", "feature-branch"),
("base", "main"),
]);
let response = client
.post(&url)
.headers(headers)
.json(&body)
.send()?;
if !response.status().is_success() {
return Err(format!("HTTP error! status: {}", response.status()).into());
}
let json: serde_json::Value = response.json()?;
println!("{}", json);
Ok(())
}
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var client = new HttpClient();
var url = $"https://api.github.com/repos/{Environment.GetEnvironmentVariable("GITHUB_OWNER")}/{Environment.GetEnvironmentVariable("GITHUB_REPO")}/pulls";
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("token", Environment.GetEnvironmentVariable("GITHUB_TOKEN"));
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
var body = new Dictionary<string, string>
{
{ "title", "Fix bug" },
{ "head", "feature-branch" },
{ "base", "main" }
};
request.Content = new StringContent(JsonSerializer.Serialize(body));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"HTTP error! status: {response.StatusCode}");
}
var json = await response.Content.ReadAsStringAsync();
Console.WriteLine(json);
}
}