I'm working with freelancer API, I want to fetch user data from this endpoint /users/0.1/users/ but I keep getting error:
error_code: "RestExceptionCodes.BAD_REQUEST"
message: "Please provide users[] or usernames[] query"
freelancer-api: List Users
function fetchV2(urlOb, headerOb, paramsOb) {
Object.keys(paramsOb).forEach(key => urlOb.searchParams.append(key, paramsOb[key]))
return fetch(urlOb, headerOb)
}
let header = {
method: 'GET',
headers: {
'freelancer-oauth-v1': *********
}
}
let url = new URL(`https://www.freelancer.com/api/users/0.1/users/`);
let params = {users:[123456,654321]};
let res = await fetchV2(url, header, params);
let data = await res.json();
console.log(data);
I also tried to query with one user_id
and but again I get the same error.
I successfully working with other routes (using the same structures) but I don't know what am I missing here! Any idea?
To others that may help them, I found out there is no standard method to pass array parameters to URLs, which means that the URLSearchParams API doesn't support parsing arrays or objects. so I built a function to make it manually:
function test() {
let url = 'https://www.freelancer.com/api/users/0.1/users/'
let paramsArray = {
users: [11111111, 22222222, 33333333],
username:['p1','p2','p3']
}
let arrString = ''
for (const property in paramsArray) {
arrString += paramsArray[property].map(item => {
return `${property}[]=${item}&`
}).join('')
}
let paramsNotArray = {
display_info: true,
status: true,
}
const params = new URLSearchParams(paramsNotArray)
url = `${url}?${arrString}${params.toString()}`
return fetch(url)
}
here is the article.