S
S
Stepan Sidorov2020-05-20 19:07:50
Python
Stepan Sidorov, 2020-05-20 19:07:50

How to send such a complex request to the smartcat site?

Hello.
I can't figure out how to send a multiplay request to the smartcat site .
I need to create a project on their site, this is the documentation I have .
Constantly returning error 500,
Here is my code:

import requests
import base64
import json


def run():
    account_id = 'f802238b-58****ab51-2989606f4745'
    key = '3_0KgI4JvUDQNLcluZYnzA4YCad'
    base_url = 'https://smartcat.ai/'
    key = account_id+ ':' + key

    projectID = "d5925bdb-******-969a-a70ef6bc703a"
   # project = base64.b64encode(bytes(project,"utf-8"))
    encoded_key = base64.b64encode(bytes(key, 'utf-8'))
    with open("project.json","r") as f:
        project = f.read()
    project = base64.b64encode(bytes(project,"utf-8"))


    headers = {
    "Authorization" : "Basic "+encoded_key.decode(),
    "project" : project.decode()
    }

    response = requests.post(base_url+'/api/integration/v1/project/create', headers=headers)

    print(response)
run()

The base64 converter is used there, it is needed because the API only accepts it :)
Question:
How can I send a request to create a new project so that it is created without errors.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shitskov, 2020-05-20
@Zarom

You overdid something. There is no need to encode anything. Requests will do everything by itself. For example,

files = {'file1': open('some_project_file1.txt', 'rb'), 'file2': open('some_project_file2.txt', 'rb')}
response = requests.post(base_url+'/api/integration/v1/project/create', data=project_json, files=files)

D
Dmitry, 2020-05-20
@LazyTalent

You know, I always thought that a person with such programming experience should read and understand texts in English without any problems.

import requests
from requests.auth import HTTPBasicAuth
import base64
import json
    
    
def run():
    account_id = 'f802238b-58****ab51-2989606f4745'
    key = base64.b64encode(bytes('3_0KgI4JvUDQNLcluZYnzA4YCad'))
    auth = HTTPBasicAuth(account_id, key)

    url = 'https://smartcat.ai/api/integration/v1/project/create'
   
    with open("project.json","r") as f:
        project = json.load(f)
   
   
    headers = {'Content-Type': 'application/json'}

    response = requests.post(url, data=project, headers=headers, auth=auth)

    print(response)
run()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question