0

I'm trying to call a PUT/DELETE request using the Stack Overflow Teams API in Python, but I'm encountering errors. As an FYI, I have a working PAT that allows me to make get requests for articles and questions. Specifically, I'm trying to create an article, update an article, and delete an article using the API, but the PUT/DELETE requests don't seem to be working. When I try to run the code, I get an error message that says "no method found with this name" or {"error_id":400,"error_message":"title","error_name":"bad_parameter"}. The title might be the issue, but I don't think it goes against the requirements (string, 5 character+ length)

I have tried running the API calls using Python's requests module, and I have checked that my authentication token is correct. I have also tried adjusting the order of the payload and double-checking that the headers are correct. However, I'm still getting errors.

Here's my code (sanitized for privacy):

import json
import os
import requests
from markdown_it import MarkdownIt

def create_article(title, access_token):
    headers = {
        "X-API-Access-Token": access_token,
        "Content-Type": "application/json"
    }
    data = {
        "title": title,
        "body": "TEST",
        "tags": "REDACTED",
        "article_type": "knowledge-article",
        "filter": "default" 
    }
    json_data = json.dumps(data)
    api_url = "https://api.stackoverflowteams.com/2.3/articles/add?team=[TEAM_NAME]"
    response = requests.post(api_url, headers=headers, data=json_data)
    print(f"API response status: {response.status_code}")
    print(f"API response text: {response.text}")
    
    return response.json()


def delete_article(access_token, artId):
    headers = {
        "X-API-Access-Token": access_token,
        "Content-Type": "application/json"
    }
    data = {
        "id": artId
    }
    json_data = json.dumps(data)
    api_url = f"https://api.stackoverflowteams.com/2.3/articles/{artId}/delete?team=[TEAM_NAME]"
    response = requests.delete(api_url, headers=headers, data=json_data)
    print(f"API response status: {response.status_code}")
    print(f"API response text: {response.text}")
    
    return response.json()

def import_single_wiki_to_teams(input_directory, access_token, test_file):
    input_file = os.path.join(input_directory, test_file)

    if os.path.isfile(input_file):
        with open(input_file, "r", encoding="utf-8") as f:
            content = f.read()

        html_content = convert_md_to_html(content)
    #   title = os.path.splitext(test_file)[0]
        title = "TeArticle"
        print(f"Title from OS is: {title!r}")

        response = create_article('Test Article', access_token)  # Use the title and html_content variables
        print(response)
    else:
        print(f"File not found: {input_file}")


input_directory = PATH
access_token = [REDACTED]
test_file = "FILE_NAME"
import_single_wiki_to_teams(input_directory, access_token, test_file)
7
  • What level of Stack Overflow for Teams product do you currently have (Free, Basic, Business, Enterprise)? The various Teams products have different levels of API support, so which product you're using is critical information. The documentation explicitly states that the Add/Edit/Delete routes are only available for Business Teams. While we might be able to give you some assistance here, Stack Overflow has quite clearly told us that we are supposed to direct all types of support requests for existing Stack Overflow for Teams to Stack Overflow's support portal.
    – Makyen
    Commented May 7, 2023 at 5:18
  • 1
    What makes you believe that you should be using PUT and/or DELETE requests? As far as I'm aware, the SE API doesn't use any PUT or DELETE routes. It only uses GET and POST. The documentation indicates that the routes for add, edit, and delete for Articles are all POST routes, which is consistent with the rest of the SE API.
    – Makyen
    Commented May 7, 2023 at 5:24
  • Hi, I work for a company that has a stack overflow for teams subscription. (This is a personal development time project). The GET articles and questions requested calls worked so far, not the others
    – Shadae
    Commented May 7, 2023 at 6:37
  • I'd assume a call to create an article would be a POST request- please correct if wrong
    – Shadae
    Commented May 7, 2023 at 6:40
  • Yes, I'd assumed your company or organization had a Stack Overflow for Teams subscription. We need to know what type of subscription it is. There's Free, Basic, Business, and Enterprise. Of those, only Stack Overflow for Teams: Business has the write routes which you're trying to use. The other Stack Overflow for Teams products could give error messages similar to what you're seeing. I believe Enterprise also has the capability, but the URLs are almost certainly different, due to it being self-hosted.
    – Makyen
    Commented May 7, 2023 at 7:08
  • That you have been able to perform GET requests from similar URLs implies that your company has one of Free, Basic, or Business, but which your company has will determine if it's even possible to use the write routes.
    – Makyen
    Commented May 7, 2023 at 7:10
  • I have a PAT and it has read-write access enabled, so I guess I should- but not sure
    – Shadae
    Commented May 7, 2023 at 13:26

1 Answer 1

0

As Makyen hinted on and confirmed by my answer on Why are Stack Exchange API methods named like this the API (both public and teams) only use the GET and POST methods of the HTTP protocol.

So instead of

api_url = f"https://api.stackoverflowteams.com/2.3/articles/{artId}/delete?team=[TEAM_NAME]"
response = requests.delete(api_url, headers=headers, data=json_data)

do

api_url = f"https://api.stackoverflowteams.com/2.3/articles/{artId}/delete?team=[TEAM_NAME]"
response = requests.post(api_url, headers=headers, data=json_data)

Contrary to what you might expect from pure, 100% compliant REST APIs the state change of the resource is in the SE API on the URL: 2.3/articles/{artId}/delete. In this case DELETE. And to have the API perform that state change for you, you POST to the endpoint.

In Python, from the Make a Request documentation you only need the get and post examples and don't need these bits as found at the end of that paragraph:

Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and OPTIONS? These are all just as simple:

r = requests.put('https://httpbin.org/put', data={'key': 'value'}) 
r = requests.delete('https://httpbin.org/delete') 
r = requests.head('https://httpbin.org/get') 
r = requests.options('https://httpbin.org/get')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .