bcb-0028
1.000
Proposed solution
```python
import requests
import json
import base64
def task_func(data, url="http://your-api-url.com"):
"""
Convert a Python dictionary into a JSON-formatted string, encode this string in base64 format,
and send it as a 'payload' in a POST request to an API endpoint.
Parameters:
data (dict): The Python dictionary to encode and send.
url (str, optional): The API endpoint URL. Defaults to "http://your-api-url.com".
Returns:
requests.Response: The response object received from the API endpoint after the POST request.
Requirements:
- requests
- json
- base64
Example:
>>> data = {'name': 'John', 'age': 30, 'city': 'New York'}
>>> response = task_func(data, url="http://example-api-url.com")
>>> print(response.status_code)
200
"""
# Convert the dictionary to a JSON-formatted string
json_string = json.dumps(data)
# Encode the JSON string in base64 format
encoded_data = base64.b64encode(json_string.encode('utf-8'))
# Create the payload dictionary with the base64-encoded data
payload = {
'payload': encoded_data.decode('utf-8')
}
# Send a POST request to the API endpoint
response = requests.post(url, json=payload)
return response
```Test output (stdout)
...... [100%] 6 passed in 0.02s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.