Error in accessing the cobalt API using Python #82

Closed
opened 2025-11-09 09:40:17 -06:00 by GiteaMirror · 4 comments
Owner

Originally created by @archie-was-taken on GitHub (Jun 23, 2023).

I want to use the API. For starters, I wrote a Python script that would download a YouTube video, e.g. https://www.youtube.com/watch?v=dQw4w9WgXcQ. However, I can't seem to get it. I provided the URL to the API using POST as application/json (as is there in the documentation), but it still didn't work.

I wrote a Python script that looks something like this:

import requests
import urllib.parse
import json

url = 'https://co.wuk.sh/api/json'
youtube_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
encoded_url = urllib.parse.quote(youtube_url)

data = json.dumps({'url': encoded_url})

response = requests.post(url, data=data)

I ran this and then at the Python shell, I did:

>>>response.text
'{"status":"error","text":"i can\'t guess what you want to download! please give me a link :("}'

The response returned an error saying that I hadn't provided a link, when I clearly had. What's the problem here? What did I do wrong?

Originally created by @archie-was-taken on GitHub (Jun 23, 2023). I want to use the API. For starters, I wrote a Python script that would download a YouTube video, e.g. https://www.youtube.com/watch?v=dQw4w9WgXcQ. However, I can't seem to get it. I provided the URL to the API using POST as application/json (as is there in the documentation), but it still didn't work. I wrote a Python script that looks something like this: ```python import requests import urllib.parse import json url = 'https://co.wuk.sh/api/json' youtube_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' encoded_url = urllib.parse.quote(youtube_url) data = json.dumps({'url': encoded_url}) response = requests.post(url, data=data) ``` I ran this and then at the Python shell, I did: ```python >>>response.text '{"status":"error","text":"i can\'t guess what you want to download! please give me a link :("}' ``` The response returned an error saying that I hadn't provided a link, when I clearly had. What's the problem here? What did I do wrong?
Author
Owner

@nununoisy commented on GitHub (Jun 26, 2023):

There’s no need to URL-encode a link in a JSON payload, so you are unintentionally sending an invalid URL - remove the urllib.parse line and just directly embed the link.

Also, you can simplify the code and send the correct Content-Type header using the json keyword argument on requests.post:

response = requests.post(url, json={'url': youtube_url})
@nununoisy commented on GitHub (Jun 26, 2023): There’s no need to URL-encode a link in a JSON payload, so you are unintentionally sending an invalid URL - remove the `urllib.parse` line and just directly embed the link. Also, you can simplify the code and send the correct `Content-Type` header using the `json` keyword argument on `requests.post`: ```python response = requests.post(url, json={'url': youtube_url}) ```
Author
Owner

@archie-was-taken commented on GitHub (Jun 27, 2023):

I did that, and it responded with a 400 HTTP status code.

>>> import app #my file is called app.py
>>> app.response
<Response [400]>

Here's what app.py looks like:

import requests
url = 'https://co.wuk.sh/api/json'
youtube_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'

response = requests.post(url, json={'url': youtube_url})
@archie-was-taken commented on GitHub (Jun 27, 2023): I did that, and it responded with a 400 HTTP status code. ```python >>> import app #my file is called app.py >>> app.response <Response [400]> ``` Here's what `app.py` looks like: ```python import requests url = 'https://co.wuk.sh/api/json' youtube_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' response = requests.post(url, json={'url': youtube_url}) ```
Author
Owner

@hyperdefined commented on GitHub (Jun 27, 2023):

You have to specify these headers.

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

And then add them to the reponse:

import requests
url = 'https://co.wuk.sh/api/json'
youtube_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
}
response = requests.post(url, json={'url': youtube_url}, headers=headers)
@hyperdefined commented on GitHub (Jun 27, 2023): You have to specify these headers. ```python headers = { "Content-Type": "application/json", "Accept": "application/json", } ``` And then add them to the reponse: ```python import requests url = 'https://co.wuk.sh/api/json' youtube_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' headers = { "Content-Type": "application/json", "Accept": "application/json", } response = requests.post(url, json={'url': youtube_url}, headers=headers) ```
Author
Owner

@archie-was-taken commented on GitHub (Jun 27, 2023):

That worked like a charm—thanks! I'm closing the issue.

@archie-was-taken commented on GitHub (Jun 27, 2023): That worked like a charm—thanks! I'm closing the issue.
Sign in to join this conversation.