Using 'requests' module for POST, I want to pick up entire JSON body from whole csv cells/values

Viewed 165

I've been trying to write a little python script to automate some API testing.

I need it to pick up the whole JSON body from a CSV or other format file, but not just a single body per file, rather iterate over all the "bodies" in it.

The way I concocted it is, each cell, or value, is an entire body. This comes from how I'm managing various tests in Google Sheets, with the whole JSON bodies in their own cells, and can then be easily exported as CSV files.

The issue is that I keep hitting "wrong format" type errors. I think the problem is that, as it's picking it up as a CSV "value", it inputs the data weirdly and that's why it won't work.

Sample "problematic" input, i.e. the value that is picked up from the CSV file, as caught through a breakpoint:

'{"post":2027,"name":"Test User","email":"email@mail.com","body":"lorem ipsum4"}'

I've already tried a lot of things. This is where I'm at right now. Further below is some sample data and more explanations.

Code:

from os import read
import requests
import csv
import json

filename = 'file.csv'

url = "https://gorest.co.in/public/v1/posts/[id]/comments"

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer tricked ya"
}

with open(filename) as cF: 
    reader = csv.reader(cF)
    for idx,row in enumerate(reader):
        for col in row:
            print("Body: ", col)
            r = requests.request("POST", url, headers = headers, json = col)
            print("Status: ", r.status_code)
            print("Status: ", r.reason)
            print("Text response: ", r.text)
            print("\nTest number: ",idx,"\n")

Sample data. In here, each row is a row in a csv file:

{"post":2027,"name":"Test User","email":"email@mail.com","body":"lorem ipsum1"}
{"post":2027,"name":"Test User","email":"email@mail.com","body":"lorem ipsum2"}
{"post":2027,"name":"Test User","email":"email@mail.com","body":"lorem ipsum3"}
{"post":2027,"name":"Test User","email":"email@mail.com","body":"lorem ipsum4"}

Sample output: ("Text response" slightly post-formatted for readability)

Body:  {"post":2027,"name":"Test User","email":"email@mail.com","body":"lorem ipsum4"}
Status:  422
Status:  Unprocessable Entity
Text response:  
{
    "meta": null,
    "data": [{
        "field": "name",
        "message": "can't be blank"
    }, {
        "field": "email",
        "message": "can't be blank"
    }, {
        "field": "body",
        "message": "can't be blank"
    }]
}

Test number:  4

The "odd" thing I've noticed, is that I can sometimes (in previous versions) input the body that is printed out (such as in the sample output) back into the JSON, when I'm using breakpoints, and that will work perfectly. So I tried using something to "capture" that "working printed body", but that wasn't really doable, or I didn't do it right.

1 Answers

csv.reader returns rows of strings, so the strings each need to be converted to a Python object for the json keyword argument in requests.request. We can use json.loads to deserialize a string.

req_obj = json.loads(col)
r = requests.request("POST", url, headers = headers, json = req_obj)
Related