Skip to content
Advertisement

How can I modify a Curl Command to run in Python with data loaded in a JSON file

I use the following Curl command below: (it works for me)

curl "https://myeducationcompany.instructure.com/api/v1/courses/9391/quizzes/196242/questions" --trace q.output -X POST -d @q1.json -H "Content-type: application/json" -H "Authorization: Bearer myBeareToken"

But I was trying to change this Curl command into a python script

Then I use this site https://curl.trillworks.com/#python to convert my Curl call into a python script.

This is the script that the site created for me:

import requests

headers = {
  'Content-type': 'application/json',
  'Authorization': 'Bearer myBeareString',
}

data = open('q1.json')
response = requests.post('https://myeducationcompany.instructure.com/api/v1/courses/9391/quizzes/196242/questions', headers=headers, data=data)

But the script is not working, I receive the error:

raise ConnectionError(err, request=request) 
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

When I try to run the script without the ‘data’ parameter,

response = requests.post('https://myeducationcompany.instructure.com/api/v1/courses/9391/quizzes/196242/questions', headers=headers)

the script runs fine. I got HTTP code 200.

I read in another response on Stackoverflow that the parameter “-d @q1.json” from Curl is not the same as the “data = open(‘q1.json’)” function on Python

I try to pass the content of the JSON file in postman, but it didn’t work.

I was able able to convert my Curl command to Python or how to use it in Postman.

How can I do that?

ps: This is the content of the “q1.json” file

{
  "question": {
    "question_name": "Pergunta",
    "question_text": "<link rel="stylesheet" href="https://instructure-uploads.s3.amazonaws.com/account_117480000000000001/attachments/582887/pucminas_subacc_graduacao_iceg_administracao.mobile.css"><span>Em uma escala de 0 a 10 (sendo 0 a menor e 10 a maior nota), quanto vocĂȘ indicaria esta disciplina para um amigo?</span>",
    "question_type": "multiple_choice_question",
    "points_possible": 0.0,
    "answers": {
      "0": {
        "answer_text": "10",
        "answer_weight": 100
      },
      "1": {
        "answer_text": "9",
        "answer_weight": 0
      },
      "2": {
        "answer_text": "8",
        "answer_weight": 0
      },
      "3": {
        "answer_text": "7",
        "answer_weight": 0
      }
    }
  }
}

Advertisement

Answer

Based in the suggestion from @fsimonjetz and @chepner

This piece of code works for me

data = open('q1.json').read()

or

data = open('q1.json')
data=data.read()

Both codes give me the same result.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement