0

I am trying to build a script that will take each json object I have and execute a requests.post successfully until it is finished. The problem I feel I may be having is running a successful loop that handles that task for me. It has been awhile since I coded some python so any insights will be helpful. Below is my data and code sets

new_df = {
    "id": 1,
    "name": "memeone",
    "smartphoneWidth": 0,
    "isHtmlCompatible": true,
    "instancesPerPage": 1,
    "isArchived": false
}, {
    "id": 1,
    "name": "memetwo",
    "smartphoneWidth": 0,
    "isHtmlCompatible": true,
    "instancesPerPage": 1,
    "isArchived": false
}

I realize it is not in an a list or within brackets [], but this is the only I can get the data to successfully post in my experience. Do I need to put it in a dataframe?

Below is my code that I am using -

test_df = pd.read_csv('dummy_format_no_id_v4.csv').rename_axis('id')
test_two_df = test_df.reset_index().to_json(orient='records')
test_three_df = test_two_df[1:-1]
new_df = test_three_df

for item in new_df:
    try:
        username = 'username'
        password = 'password'
        headers = {"Content-Type": "application/json; charset=UTF-8"}
        response = requests.post('https://api.someurl.com/1234/thispath', data=new_df, headers=headers, auth=(username, password))
        print(response.text)
    except:
        print('ERROR')

the issue here is it will post first json object ("name": "memeone") successfully, but won't post the next one ("name":"memetwo")? How can I get it to iterate and also post the next json object? Must it be in a dataframe?

Thank you for any advice in advance. Apologies if my code is bad.

  • ***Update*** I was reading up and essentially, what I am trying to do is this: https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests - but difference is, my data set has two json Objects where as in this example, it is just one. When trying the approach mentioned in the link, I get `{"errors":{"":["A non-empty request body is required."]}`ERROR. – knowledgealways Apr 03 '22 at 17:07

1 Answers1

0

Actually, package requests itself has a json parameter that has been provided, and you can use that. instead of using data or saving it in dataframe, you can use like this :

for item in new_df:
    try:
        headers = {"Content-Type": "application/json; charset=UTF-8"}
        response = requests.post(
            f"https://httpbin.org/anything/{new_df}", json=new_df, headers=headers
        )
        # put break statement to terminate the loop
        print(response.text)
        break
    except Exception as e:
        raise Exception(f"Uncaught exception error {e}")
  • Hello! Thank you for this - i adjusted my code however, received a Bad Request - Invalid URL, HTTP Error 400. The request URL is invalid , Error response. I think it is because I can't pass my objects via URL string to the endpoint? Maybe I am mis-understanding? Appreciate any assistance! – knowledgealways Apr 02 '22 at 17:56