0

I am using the following code in order to retrieve a json from an endpoint:

import json

from playwright.sync_api import sync_playwright

API_URL = 'url'

with sync_playwright() as p:
    browser = p.webkit.launch(headless=True)
    page = browser.new_page()
    page.goto(API_URL)
    html = page.evaluate('document.querySelector("pre").innerText')

try:
    data = json.loads(html)
except:
    data = None

print(data)

That works for GET requests, but what should I do in order to perform a POST request? The url that I am testing for the POST request is API_URL = 'https://www.soraredata.com/api/players/price-graph' and

POST_data="""
      {"player_id": "8244483723335967330548112608338433168438009441877418350246645421695467333117",        "scarcity":"Limited",        "start_date":"2021-12-21T11:27:04.946521Z",        "end_date":"2022-01-21T11:27:04.946521Z",        "currency":"Ξ"}   
    """

EDIT: After hardkoded answer,I edited my code, but still not working (status=403 status_text='Forbidden) even if that request performed using a normal browser seems legit.

import json
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.webkit.launch(headless=False)
    context = browser.new_context(base_url="https://www.soraredata.com")
    api_request_context = context.request
    page = context.new_page()

    to_send = """
      {
        "player_id": "8244483723335967330548112608338433168438009441877418350246645421695467333117",
        "scarcity":"Limited",
        "start_date":"2021-12-26T08:11:09.860Z",
        "end_date":"2022-01-25T08:11:08.637Z",
        "currency":"Ξ"
    }   
    """

    response = api_request_context.post(
        "/api/players/price-graph",
        headers={
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json"
        },
        data=to_send,
    )


try:
    print(response)
    data = json.loads(response)
except:
    # Still might fail sometimes
    data = None

print(data) 
SctALE
  • 509
  • 2
  • 10
  • 30

1 Answers1

0

You can use the API request feature:

browser = playwright.chromium.launch()
context = browser.new_context(base_url="https://api.github.com")
api_request_context = context.request
page = context.new_page()

response = api_request_context.post(
  "/user/repos",
  headers={
      "Accept": "application/vnd.github.v3+json",
      "Authorization": f"token {API_TOKEN}",
  },
  data={"name": REPO},
)
hardkoded
  • 18,915
  • 3
  • 52
  • 64