Python Forum
requests.post not sending post values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
requests.post not sending post values
#1
Hello everyone,

As the title says, none of the post values are being sent

Here is the Python script:


 params = { 'code' : code, 'client_id': client_id, 'client_secret': client_secret, 'redirect_uri': redirect_uri, 'grant_type': 'authorization_code', } headers = { 'content-type': 'application/x-www-form-urlencoded' } response = requests.post(testurl+"?test=1", headers=headers, json = params) print(response.text) print(dir(response))
Here is the php code of the site receiving it

<?php $r = var_export($_POST, true); print_r(json_encode($r)); ?>
all I get back is "Array()"

Does anyone know what I am doing wrong?
Reply
#2
Not that I really know about this stuff, but the example below works.

If you are not afraid of problems, post your url for testing.

I don't know what the php looks like on the test site used below, but it gives a decent response.

import json import requests url = 'https://httpbin.org/post' payload = {'key1': 'value1', 'key2': 'value2'} headers = {'Content-Type': 'application/json'} response = requests.post(url, json=payload, headers=headers) print(response.json())
This returns:

Output:
{'args': {}, 'data': '{"key1": "value1", "key2": "value2"}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '36', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.31.0', 'X-Amzn-Trace-Id': 'Root=1-694b2fc2-511bcc222a3b3c6e0019f60b'}, 'json': {'key1': 'value1', 'key2': 'value2'}, 'origin': '114.221.165.190', 'url': 'https://httpbin.org/post'}
Reply
#3
Hi,

you are setting the wrong content-type for JSON... If you want to send JSON, you need to set the content-type correctly. Or provide the payload in the right way for request.

See request documentation and MDN documentation on content-types for POST requests.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST `api` 415 (Unsupported Media Type) Dexty 2 8,292 Aug-09-2023, 02:42 AM
Last Post: OEVFyeesZbrU
Question Python request (post/get) Drunknmonkie 1 4,570 Jan-19-2023, 02:02 PM
Last Post: prvncpa
  Post request not saving in DRF Dexty 0 1,750 Jun-03-2022, 12:35 PM
Last Post: Dexty
  POST requests - different requests return the same response Default_001 3 3,751 Mar-10-2022, 11:26 PM
Last Post: Default_001
  Post Python to HTML/CSS Extra 3 3,874 Feb-17-2022, 06:07 PM
Last Post: snippsat
Brick how to work with 2 or more related tables in a Post request ikurorox 0 2,449 Dec-05-2021, 01:01 AM
Last Post: ikurorox
  POST Reply to Ajax/jQuery (mostly an HTML question) gw1500se 5 4,326 Nov-18-2021, 02:44 PM
Last Post: gw1500se
  Post HTML Form Data to API Endpoints Dexty 0 2,233 Nov-11-2021, 10:51 PM
Last Post: Dexty
  requests.post() does work Alto 1 3,056 Aug-13-2021, 07:58 AM
Last Post: ndc85430
  Scroll Down Post Instagram Likes rmiguelantunes74 0 4,158 Oct-05-2020, 07:38 PM
Last Post: rmiguelantunes74

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.