読み込み中...
読み込み中...
curlをPython requestsに変換
const response = await fetch("https://api.example.com/data", { method: "POST", headers: { "Content-Type": "application/json" }, body: "{\"key\":\"value\"}" }); const data = await response.json(); console.log(data);
import requests response = requests.post( "https://api.example.com/data", headers={ "Content-Type": "application/json" }, data="{\"key\":\"value\"}" ) print(response.status_code) print(response.json())
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"key\":\"value\"}"); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); echo $httpCode . "\n"; echo $response . "\n";
package main import ( "fmt" "io" "net/http" "strings" ) func main() { body := strings.NewReader("{\"key\":\"value\"}") req, err := http.NewRequest("POST", "https://api.example.com/data", body) if err != nil { panic(err) } req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { panic(err) } fmt.Println(resp.StatusCode) fmt.Println(string(respBody)) }