Home > Mobile >  Unable to POST request correctly
Unable to POST request correctly

Time:09-08

I wanted to shorten the link through the wklej API, they have documentation on the site but only for php, I can’t understand what the problem is. Here is an example request from their website:

https://wklej.to/api/url/add

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://wklej.to/api/url/add",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 2,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer HERE IS MY API KEY",
        "Content-Type: application/json",
    ),
    CURLOPT_POSTFIELDS => '{
    "url": "https:\/\/google.com",
    "custom": "google",
    "password": "mypass",
    "expiry": "2020-11-11 12:00:00",
    "type": "splash",
    "metatitle": "Not Google",
    "metadescription": "Not Google description",
    "metaimage": "https:\/\/www.mozilla.org\/media\/protocol\/img\/logos\/firefox\/browser\/og.4ad05d4125a5.png",
    "geotarget": [
        {
            "location": "Canada",
            "link": "https:\/\/google.ca"
        },
        {
            "location": "United States",
            "link": "https:\/\/google.us"
        }
    ],
    "devicetarget": [
        {
            "device": "iPhone",
            "link": "https:\/\/google.com"
        },
        {
            "device": "Android",
            "link": "https:\/\/google.com"
        }
    ],
    "languagetarget": [
        {
            "language": "en",
            "link": "https:\/\/google.com"
        },
        {
            "language": "fr",
            "link": "https:\/\/google.ca"
        }
    ],
    "parameters": [
        {
            "name": "aff",
            "value": "3"
        },
        {
            "device": "gtm_source",
            "link": "api"
        }
    ]
}',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;


I tried making a request like this:

r = requests.post("https://wklej.to/api/url/add", data = {"api": "HERE IS MY API KEY", "url": "https://amazon.com"})

but in response i get this:

{"error":1,"message":"A valid API key is required to use this service."}

I recently started learning python and would like to know what I did wrong, if I understand correctly then I need to send a request in JSON format. By the way, their documentation states that in the request you only need to specify a link for the shortener and an api key, everything else is optional.

update

now i do this:

headers = {
    'Authorization': 'Bearer MY_API_KEY',
    'Content-Type': 'application/json',
}

data = {"url": "https://amazon.com"}


r = requests.post("https://wklej.to/api/url/add", headers=headers, data=data)

and response is :

{"error":1,"message":"Missing required parameter: url"}

Why is it missing ? I have set this in "data"

CodePudding user response:

Try this instead:

r = requests.post("https://wklej.to/api/url/add", headers=headers, json=data)

The data argument will be form-encoded, while the json argument serializes the passed value to json. See documentation here.

  • Related