0

I am trying to imitate a authenticated JSON request. I am using the Requests module to do so.

import requests
import json

url = "https://"
headers = {
    'Host': 'example.com',
    'content-type': 'application/json',
    'Connection': 'keep-alive'
}

data = {
"asdsdsd": {
    "AppName": "esdf",
    "Language": "EN",
    "fsef": [
        {
            "sfddf": [
                {
                    "sdfsdfsdf": "sdfsdfsdf"
                }
            ]
        }
    ]
},
"sdfdf": {
    "sdfsdf": "sdfsdfsdf"
}
}

r = requests.post(url, data=json.dumps(data), headers=headers ,verify=False)

How do i include the authenticated cookie into this.

Can I open this request using a browser(with the headers & data) then i would be able to use the authenticated cookie of the browser.

Is there any way to do this ?

3 Answers 3

1

Use a Session object to manage cookies.

s = requests.Session()

# load initial page to get session cookies set, perhaps a CSRF token
loginform = s.get(loginurl)

# post login information to the form
s.post(someurl, data={...})

# post JSON with session with authentication cookie
s.post(someurl, ...)
Sign up to request clarification or add additional context in comments.

2 Comments

I am not logging in using the script, i need to send a authenticated request in between, Can I use the cookieLib libraray to get the cookies from the mozila cookie jar ?
@VinodK: That depends on the browser, and depending on how you want to support various browsers and end users could get a lot more complicated than just using a session and parsing the login page.
0
headers = {
'Host': 'example.com',
'content-type': 'application/json',
'Connection': 'keep-alive'
}

session = requests.session()
session.headers=headers

you can use session for post

session.post(url, data=json.dumps(data) ,verify=False)

1 Comment

The headers structure is not valid Python.
0

You can pass your data directly into the form of a cookie in your request like this :

data = {
    "sessionid":"yoursessionidorsomethingelseidontcarelolwtfhelloworld"
}
url = "https://youramazingurl.com/list_some_private_data"
res = requests.get(url, cookies=data)

Done :)

6 Comments

Then why didn't he use the cookie argument ? .-.
Perhaps the OP doesn't know about the cookie argument? Why didn't you suggest the request.Session() option to handle cookies between requests?
Because if the OP doesn't need to handle sessions but still needs to make a request with a cookie auth, then it could be simpler ? I don't know, that fits the OP needs so why shoud I complicate my answer ? ^^
Right, the OPs needs weren't clear to me. I was more focussing on the 'there is a login form and a cookie' issue.
What i want to know is whether i can access the authenticated cookies, or whether i can open the same request (with header & data) using a browser. btw what does OP stand for ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.