0

After going through several posts in the matter of JSON response.content or response.text; I've been incapable of finding a solution to my problem and so I'm turning to you.

To put it simple, I have to define a function that is meant to ascertain whether status.code is correct and, if so, return the values of the headers along with their respective contents. If the status.code isn't valid, then it has to display the error code.

My attempt at this:

import json
import requests

def API(base_url): 
    response = requests.get(base_url) #Connecting to URL
    if response.status_code == 200:#If connection is successful
        head = json.dumps((dict(response.headers)), sort_keys=True, indent=3, separators=(',', ': ')) #Head will contain the header values and sort them
        cont = json.dumps(str(response.content), sort_keys=True, indent=3, separators=(',', ': ')) #Cont will do the same for content
        return head, cont
    else:
        return response.status_code, response.reason    

print(API('http://wikipedia.org'))
print(API('http://google.com/thehearthisflat'))

By doing this, I realized that 'cont' variable isn't being formatted as JSON. In fact, it keeps returning the original values as if it were python code. However, if you ask the function to return only the values of 'head' it displays the JSON format perfectly for the headers.

This fact leads me to question whether I may be using a wrong argument (json.dumps) to have the response.content correctly formatted and sorted.

Could you please help me understand what am I doing wrong here?

Thank you so much in advance!

MadMarx17
  • 71
  • 7
  • A response's content may not be json and therefore cannot be represented as json, you can check the type by accessing the 'content-type' header: `resopnse.headers['content-type']` – joshmeranda Nov 19 '21 at 19:52
  • note you could also use requests method `raise_for_status()` if you want to check if the request was a success or not. The main benefit of doing that is the error already contains status and reason IIRC, so no need to format this yourself. – rv.kvetch Nov 19 '21 at 20:02
  • @joshmeranda Hi Josh, thanks a lot. Checked this and realized data type is text/html. That being said, is there any way to display it in JSON format? Anyhow, whenever I ask 'return' to return both variables, headers aren't being displayed in JSON format so I figure the second argument 'cont' has something to do with that. Any clue? – MadMarx17 Nov 19 '21 at 20:38
  • @MadMarx17 technically I believe so if you follow [this answer](https://stackoverflow.com/questions/191536/converting-xml-to-json-using-python); however, that is generally not a great idea since HTML and JSON are meant for different things. You could add some web scrapping to pull out the target information and put that into json – joshmeranda Nov 19 '21 at 20:41
  • @joshmeranda, You're absolutely right, I managed to come up with a workaround after going through the post you shared, I found another one in the comments section that will do the job: https://stackoverflow.com/questions/43469412/convert-html-source-code-to-json-object – MadMarx17 Nov 19 '21 at 22:43
  • if you return two values then you get tuple and `print()` for tuple use special method to show it useful for debuging. You should display every value separatelly - `head, cont = API(...)` and `print(head, cont)` (but it will start `cont` in the same line as end of `head`) or `print(head)`, `print(cont)` or `result = API(...)` and `print(result[0])`, `print(result[1])` OR if you have only strings then you can use `print( "\n".join(result) )`. Eventually you can use `*` to unpack it and `print( *result )` or `print( *API(...) )` but again it will start `cont` in the same line as end of `head` – furas Nov 20 '21 at 00:18

0 Answers0