Setting cookies
Cookies can be passed as array in cookies
parameter.
Remember to encode this parameter like in the examples below.
Cookies syntax
Cookies is an array of objects.
"name"
and "value"
are required fields, other are optional.
Example array with two cookies:
[
{
"name": "cookie1",
"value": "cookie1value"
},
{
"name": "cookie2",
"value": "cookie2value",
"domain": "example.com",
"path": "/",
"expires": 100000,
"secure": true,
"httpOnly": false,
"sameSite": "Lax"
}
]
name
Required string. Cookie's name.
value
Required string. Cookie's value.
domain
Optional string. Cookie's domain.
You can either set domain
and path
or url
.
If you don't set any of the three, we use domain equal to the domain of the desired URL and path to /
.
For example, if you scrape https://example.com/some/path?arg1=value1&arg2=value2
and don't specify cookie's URL, domain nor path, then cookie will have its domain
set to example.com
and path to /
.
See more info on MDN.
path
Optional string. Cookie's path.
You can either set domain
and path
or url
.
If you don't set any of the three, we use domain equal to the domain of the desired URL and path to /
.
For example, if you scrape https://example.com/some/path?arg1=value1&arg2=value2
and don't specify cookie's URL, domain nor path, then cookie will have its domain
set to example.com
and path to /
.
See more info on MDN.
url
Optional string. Cookie's URL.
You can either set domain
and path
or url
.
If you don't set any of the three, we use domain equal to the domain of the desired URL and path to /
.
For example, if you scrape https://example.com/some/path?arg1=value1&arg2=value2
and don't specify cookie's URL, domain nor path, then cookie will have its domain
set to example.com
and path to /
.
See more info on MDN.
expires
Optional integer. Cookie's expiry timestamp.
Specifies cookie lifetime in Unix time in seconds. See more info about cookie's lifetime on MDN.
secure
Optional boolean.
See more info on MDN.
httpOnly
Optional boolean.
See more info on MDN.
sameSite
Optional string. One of "Strict"
, "Lax"
, or "None"
.
Example
The following example sets two cookies:
- cookie1-name:
cookie1-value
, - cookie2-name:
cookie2-value
.
- cURL
- Python
- JavaScript
curl -G \
--data-urlencode 'url=https://example.com' \
--data-urlencode 'cookies=[{"name": "cookie1-name", "value": "cookie1-value"}, {"name": "cookie2-name", "value": "cookie2-value"}]' \
'https://scraping.narf.ai/api/v1/?api_key=[your API key]'
import requests
import json
from urllib.parse import quote_plus
url = quote_plus("https://example.com")
api_key = "[your API key]"
cookies = quote_plus(
json.dumps([{
"name": "cookie1-name"
"value": "cookie1-value"
}, {
"name": "cookie2-name"
"value": "cookie2-value"
}])
)
print(requests.get(f"https://scraping.narf.ai/api/v1/?api_key={api_key}&url={url}&cookies={cookies}").content)
import axios from "axios";
const url = encodeURIComponent("https://example.com");
const apiKey = "[your API key]";
const cookies = encodeURIComponent(
JSON.Stringify([{
name: "cookie1-name",
value: "cookie1-value"
}, {
name: "cookie2-name",
value: "cookie2-value"
}])
);
const response = await axios.get(
`https://scraping.narf.ai/api/v1/?api_key=${apiKey}&url=${url}&cookies=${cookies}`
);
console.log(response.data);