Home > Blockchain >  Getting error when using stackexchange api
Getting error when using stackexchange api

Time:10-15

I am getting this error when I use the edit question API:

{"error_id":400,"error_message":"site is required","error_name":"bad_parameter"}

This is my code:

r = requests.post(f"https://api.stackexchange.com/2.3/questions/{qid}/suggested-edit/add", json={"access_token": at, "site": "stackoverflow", "title":t,"body":str(b),"key":key})

CodePudding user response:

You are using the requests.post function incorrectly. If you'd like to send parameters with your request, use the params argument and pass the parameters as a dictionary.

r = requests.post(f"https://api.stackexchange.com/2.3/questions/{qid}/suggested-edit/add", params={"access_token": at, "site": "stackoverflow", "title":t,"body":str(b),"key":key})

CodePudding user response:

From the official docs page I'm getting the javascript request structure as this

await fetch("https://api.stackexchange.com/2.3/questions/11/suggested-edit/add", {
    "credentials": "include",
    "headers": {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0",
        "Accept": "*/*",
        "Accept-Language": "en-US,en;q=0.5",
        "Content-Type": "application/x-www-form-urlencoded",
        "X-Requested-With": "XMLHttpRequest",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin"
    },
    "referrer": "https://api.stackexchange.com/docs/create-question-suggested-edit",
    "body": "id=11&body=111&key=111&access_token=111&preview=true&filter=default&site=stackoverflow",
    "method": "POST",
    "mode": "cors"
});

In Python you can use like this

r = requests.post(f"https://api.stackexchange.com/2.3/questions/{qid}/suggested-edit/add",headers={"Content-Type": "application/x-www-form-urlencoded"}, data={"access_token": at, "site": "stackoverflow", "title":t,"body":str(b),"key":key,"id":qid})
  • Related