SoFunction
Updated on 2025-03-10

Sample code for sending HTTP POST requests in Python request library

This code uses PythonrequestsThe library sends HTTP POST requests, sends data to the local server's API, and processes the response. Next, I will explain this code step by step and answer it in Chinese.

import requests 
import json 

url = "http://localhost:11434/api/generate"

headers = {
    "Content-Type": "application/json"
}

data = {
    "model": "qwen2:0.5b",
    "prompt": "Why is the sky black? Answer in Chinese.",
    "stream": False
}

response = (url, headers=headers, data=(data))
print()

if response.status_code == 200:
    response_text = 
    data = (response_text)
    actual_response = data["response"]
    print(actual_response)
else:
    print("error:", response.status_code, )

{
    "model": "qwen2:0.5b",
    "created_at": "2024-08-22T06:20:16.768091631Z",
    "response": "Why is the sky black? Because when light passes through the atmosphere, it will be affected by different scattering phenomena. Specifically, atmospheric molecules (such as nitrogen, oxygen, etc.) and water vapor prevent most of the wavelengths of light in the sun, resulting in only red, orange, yellow, green, and blue colorful lights being scattered in the sunlight; while blue and purple lights are absorbed and reflected back. The result is that the sky appears black.",
    "done": true,
    "done_reason": "stop",
    "context": [
        151644,872,198,10234,374,279,12884,3691,30,21806,304,8453,13,151645,198,151644,77091,198,101916,100678,20412,104723,9370,11319,99519,109587,109239,105797,99371,13343,3837,36993,100683,101970,99632,99759,102060,99564,1773,100398,99883,3837,105797,102388,9909,29524,109958,99180,5373,115893,49567,7552,33108,52510,101494,99180,107345,34187,104166,101047,101212,99804,45861,109587,3837,100673,101281,99225,15946,101043,99425,5373,107678,5373,99789,5373,99679,5373,100400,75108,100847,99566,38035,9370,109587,99250,99632,99759,24968,68536,105681,5373,111413,109587,46448,99250,104460,62926,111192,104748,1773,59151,99486,101916,107433,104723,1773
    ],
    "total_duration": 3086970885,
    "load_duration": 48652771,
    "prompt_eval_count": 18,
    "prompt_eval_duration": 53432000,
    "eval_count": 90,
    "eval_duration": 2943180000
}

Import the library:

import requests 
import json

The code was imported firstrequestslibrary is used to send HTTP requests, andjsonLibrary is used to process JSON data.

Define URL and request header:

url = "http://localhost:11434/api/generate"

headers = {
    "Content-Type": "application/json"
}

Here is the requested target URL, that is, the API interface running on the local serverhttp://localhost:11434/api/generate. At the same time, the request header is defined and specifiedContent-Typeforapplication/json, means that the data passed in the request body is JSON.

Create request body data:

data = {
    "model": "qwen2:0.5b",
    "prompt": "Why is the sky black? Answer in Chinese.",
    "stream": False
}

dataThe dictionary contains three key-value pairs:

  • "model": Specify the model to use, here isqwen2:0.5b
  • "prompt": The prompt question provided, here is "Why is the sky black? Answer in Chinese."
  • "stream": Indicates whether you want the server to return data in streaming mode, set here toFalse

Send a POST request and get the response:

response = (url, headers=headers, data=(data))
print()

use()The method sends a POST request, passing the target URL, request header and request body data. Then print the server's response content.

Processing response:

if response.status_code == 200:
    response_text = 
    data = (response_text)
    actual_response = data["response"]
    print(actual_response)
else:
    print("error:", response.status_code, )

First check whether the status code of the response is 200 (indicating that the request is successful). If successful, parse the response text in JSON format and extract the actual answer content. Otherwise, print the error message.

Sample output:

If the server is running normally and can handle requests, the output might be:

{
    "response": "Because the sky is not illuminated by light without the sun, it looks black."
}

The program will extract and print"Because the sky is not illuminated by light without the sun, it looks black."